PostGIS  3.2.2dev-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 127 of file lwalgorithm.c.

128 {
129  POINT2D C;
130  double radius_A, circumference_A;
131  int a2_side, clockwise;
132  double a1, a3;
133  double angle;
134 
135  if ( lw_arc_is_pt(A1, A2, A3) )
136  return 0.0;
137 
138  radius_A = lw_arc_center(A1, A2, A3, &C);
139 
140  /* Co-linear! Return linear distance! */
141  if ( radius_A < 0 )
142  {
143  double dx = A1->x - A3->x;
144  double dy = A1->y - A3->y;
145  return sqrt(dx*dx + dy*dy);
146  }
147 
148  /* Closed circle! Return the circumference! */
149  circumference_A = M_PI * 2 * radius_A;
150  if ( p2d_same(A1, A3) )
151  return circumference_A;
152 
153  /* Determine the orientation of the arc */
154  a2_side = lw_segment_side(A1, A3, A2);
155 
156  /* The side of the A1/A3 line that A2 falls on dictates the sweep
157  direction from A1 to A3. */
158  if ( a2_side == -1 )
159  clockwise = LW_TRUE;
160  else
161  clockwise = LW_FALSE;
162 
163  /* Angles of each point that defines the arc section */
164  a1 = atan2(A1->y - C.y, A1->x - C.x);
165  a3 = atan2(A3->y - C.y, A3->x - C.x);
166 
167  /* What's the sweep from A1 to A3? */
168  if ( clockwise )
169  {
170  if ( a1 > a3 )
171  angle = a1 - a3;
172  else
173  angle = 2*M_PI + a1 - a3;
174  }
175  else
176  {
177  if ( a3 > a1 )
178  angle = a3 - a1;
179  else
180  angle = 2*M_PI + a3 - a1;
181  }
182 
183  /* Length as proportion of circumference */
184  return circumference_A * (angle / (2*M_PI));
185 }
#define LW_FALSE
Definition: liblwgeom.h:108
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
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:237
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:63
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:114
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:50
double y
Definition: liblwgeom.h:404
double x
Definition: liblwgeom.h:404