PostGIS  3.4.0dev-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 116 of file lwalgorithm.c.

117 {
118  POINT2D C;
119  double radius_A, circumference_A;
120  int a2_side, clockwise;
121  double a1, a3;
122  double angle;
123 
124  if ( lw_arc_is_pt(A1, A2, A3) )
125  return 0.0;
126 
127  radius_A = lw_arc_center(A1, A2, A3, &C);
128 
129  /* Co-linear! Return linear distance! */
130  if ( radius_A < 0 )
131  {
132  double dx = A1->x - A3->x;
133  double dy = A1->y - A3->y;
134  return sqrt(dx*dx + dy*dy);
135  }
136 
137  /* Closed circle! Return the circumference! */
138  circumference_A = M_PI * 2 * radius_A;
139  if ( p2d_same(A1, A3) )
140  return circumference_A;
141 
142  /* Determine the orientation of the arc */
143  a2_side = lw_segment_side(A1, A3, A2);
144 
145  /* The side of the A1/A3 line that A2 falls on dictates the sweep
146  direction from A1 to A3. */
147  if ( a2_side == -1 )
148  clockwise = LW_TRUE;
149  else
150  clockwise = LW_FALSE;
151 
152  /* Angles of each point that defines the arc section */
153  a1 = atan2(A1->y - C.y, A1->x - C.x);
154  a3 = atan2(A3->y - C.y, A3->x - C.x);
155 
156  /* What's the sweep from A1 to A3? */
157  if ( clockwise )
158  {
159  if ( a1 > a3 )
160  angle = a1 - a3;
161  else
162  angle = 2*M_PI + a1 - a3;
163  }
164  else
165  {
166  if ( a3 > a1 )
167  angle = a3 - a1;
168  else
169  angle = 2*M_PI + a3 - a1;
170  }
171 
172  /* Length as proportion of circumference */
173  return circumference_A * (angle / (2*M_PI));
174 }
#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:226
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:62
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:103
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390

References lw_arc_center(), lw_arc_is_pt(), LW_FALSE, lw_segment_side(), LW_TRUE, p2d_same(), POINT2D::x, and POINT2D::y.

Referenced by ptarray_arc_length_2d(), and test_lw_arc_length().

Here is the call graph for this function:
Here is the caller graph for this function: