Brieg - 7 months ago 99

C++ Question

How to draw circle or semi circle or arc from start(B) to end(C) point giving:

- A(Ax,Ay) - polyline
- B(Bx,By) - start point
- C(Cx,Cy) - end point
- M(Mx,My) - center of circle or semi circle or arc
- r - radius
- startAngle = atan2(B.y - M.y, B.x - M.x) - in radians
- endAngle = atan2f(C.y - M.y, C.x - M.x) - in radians
- segments = 80

Unknow values:

- clockwise or anticlockwise direction

Requirements

- Arc must be drawing from point B to C

Can someone please provide me equation for drawing an arc in OpenGL?

Will

Please give me a some light to solve this.

I wrote this, issuse is now:

Wrong draw direction. Please see picture. Semi circle have to be bottom of the line and not on the top.

In the case of 1 everything is fine.

In the case of 2 draw direction should be bottom(CCW)

`float startAngle = atan2(B.y - centerArc.y, B.x - centerArc.x);`

float endAngle = atan2(C.y - centerArc.y, C.x - centerArc.x);

if (endAngle < startAngle)

{

endAngle += 2.0f * PI;

}

glBegin(GL_LINE_STRIP);

for (float angle = startAngle; angle <= endAngle; angle = angle + 0.01f)

{

float x = centerArc.x + cos(angle) * r;

float y = centerArc.y + sin(angle) * r;

glVertex2f(x, windowHeight - y);

}

glEnd();

Answer

The equation for points on a circle, semi-circle or circle arc is the same for all cases. The only variable that changes is the range in which the angle lies.

In pseudocode this looks as follows:

```
for angle in [startAngle, endAngle]
p_on_circle = M + r * (cos(angle), sin(angle))
```

This assumes, that angle is given in radians, so one might want to just use

```
startAngle = atan2(B.y - M.y, B.x - M.x)
endAngle = atan2f(C.y - M.y, C.x - M.x)
```

**Edit**

Since atan2 produces angles in [-pi, pi] which makes some problems when `endAngle`

is smaller than `startAngle`

one has to make sure that the `endAngle`

is always larger than the `startAngle`

. This can, for example, be done by:

```
if (endAngle < startAngle)
endAngle += 2*pi;
```