PostGIS  3.6.1dev-r@@SVN_REVISION@@

◆ lw_arc_length()

double lw_arc_length ( const POINT2D A1,
const POINT2D A2,
const POINT2D A3 
)

Returns the length of a circular arc segment.

Definition at line 134 of file lwalgorithm.c.

135 {
136  POINT2D C;
137  double radius_A, circumference_A;
138  int a2_side, clockwise;
139  double a1, a3;
140  double angle;
141 
142  if ( lw_arc_is_pt(A1, A2, A3) )
143  return 0.0;
144 
145  radius_A = lw_arc_center(A1, A2, A3, &C);
146 
147  /* Co-linear! Return linear distance! */
148  if ( radius_A < 0 )
149  {
150  double dx = A1->x - A3->x;
151  double dy = A1->y - A3->y;
152  return sqrt(dx*dx + dy*dy);
153  }
154 
155  /* Closed circle! Return the circumference! */
156  circumference_A = M_PI * 2 * radius_A;
157  if ( p2d_same(A1, A3) )
158  return circumference_A;
159 
160  /* Determine the orientation of the arc */
161  a2_side = lw_segment_side(A1, A3, A2);
162 
163  /* The side of the A1/A3 line that A2 falls on dictates the sweep
164  direction from A1 to A3. */
165  if ( a2_side == -1 )
166  clockwise = LW_TRUE;
167  else
168  clockwise = LW_FALSE;
169 
170  /* Angles of each point that defines the arc section */
171  a1 = atan2(A1->y - C.y, A1->x - C.x);
172  a3 = atan2(A3->y - C.y, A3->x - C.x);
173 
174  /* What's the sweep from A1 to A3? */
175  if ( clockwise )
176  {
177  if ( a1 > a3 )
178  angle = a1 - a3;
179  else
180  angle = 2*M_PI + a1 - a3;
181  }
182  else
183  {
184  if ( a3 > a1 )
185  angle = a3 - a1;
186  else
187  angle = 2*M_PI + a3 - a1;
188  }
189 
190  /* Length as proportion of circumference */
191  return circumference_A * (angle / (2*M_PI));
192 }
#define LW_FALSE
Definition: liblwgeom.h:94
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
double lw_arc_center(const POINT2D *p1, const POINT2D *p2, const POINT2D *p3, POINT2D *result)
Determines the center of the circle defined by the three given points.
Definition: lwalgorithm.c:244
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:70
int lw_arc_is_pt(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns true if arc A is actually a point (all vertices are the same) .
Definition: lwalgorithm.c:121
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:57
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390