How to draw Arc know only startpoint and endpoint in 2D?

1.7k Views Asked by At

I have got short question: How to draw Arc in 2 demension know only start and end point in OpenGL?

Actually, something already wrote, however, this algorithm does not accomplish its purpose. Here we go:

 1. Get startXY
 2. Get endXY
 3. Const num_segments = numeric below than 0
 4. Calc midPoint.SetXY((start.x + end.x) / 2, (start.y + end.y) / 2)
 5. Calc diameter = sqrt(pow((end.x - start.y), 2) + pow((end.y -
    start.y), 2));
 6. Calc start_angle = atan2(start.y - midPoint.y, start.x -
    midPoint.x);
 7. Calc end_angle = atan2(end.y - midPoint.y, end.y - midPoint.x);
 8. Calc r = sqrt(((midPoint.x - start.x)*(midPoint.x - start.x)) +
    ((midPoint.y - start.y)*(midPoint.y - start.y)));
 9. Calc theta = end_angle / num_segments;
 10. Calc tangetial_factor = tanf(theta);
 11. Calc float radial_factor = cosf(theta);
 12. Calc x = r * cosf(start_angle); //we now start at the start angle
 13. Calc y = r * sinf(start_angle);
 14. for (int ii = 0; ii < num_segments; ii++)
        drawXY(x + midPoint.x, windowHeight - (y + midPoint.y));
        float tx = -y;
        float ty = x;
        x += tx * tangetial_factor;
        y += ty * tangetial_factor;
        x *= radial_factor;
        y *= radial_factor;
 15. endFor

Where I made a mistake? Is there a faster way to draw semi-circle?

4

There are 4 best solutions below

1
On

The formula for the equation of the circle using $(x_{start}, y_{start})$ and $(x_{end}, y_{end})$ as two endpoints of its diameter is:-

$$(x - x_{start})(x - x_{end}) + (y - y_{start})(y - y_{end}) = 0$$

It saves some time than going through the process of finding the midpoint.

1
On

Let $(x_s,y_s)$ and $(x_e,y_e)$ denote the coord. of start and end points. Let $(x_m,y_m)$ be those of their midpoint.

Choose $\varepsilon=+1$ (resp. $\varepsilon=-1$) if you want a direct (resp. inverse) half circle.

This half-circle can be plotted with the simple formulas:

$$\cases{x=x_m+(x_s-x_m) \cos(t)+\varepsilon(y_s-y_m)\sin(t)\\y=y_m+(y_s-y_m) \cos(t)-\varepsilon(x_s-x_m)\sin(t)} \ \ \text{for} \ \ t=0...\pi \ \ \text{with a certain time step.}$$

Simple explanation : formulas above come from the following description

$$\tag{1}\pmatrix{x(t)-x_m\\y(t)-y_m}=\cos(t)\pmatrix{(x_s-x_m)\\(y_s-y_m)}+\sin(t)\pmatrix{\varepsilon(y_s-y_m)\\-\varepsilon(x_s-x_m)}$$

by complete "analogy" with the description of (half) unit circle as being the set:

$$\pmatrix{x\\y}=\cos(t)\vec{i}+\sin(t)\vec{j},$$

with the replacement of $\vec{i}$ and $\vec{j}$ by vectors $\vec{I}$ and $\vec{J}$, adapted to the half circle to be drawn. Vector $\vec{I}$ is quite natural ; $\vec{J}$ is chosen such as being orthogonal to $\vec{I}$ (dot product = 0) with the same norm, but orientation depending on $\varepsilon$).

Remark: To underline the "transformational aspect", formula (1) could have been written in the equivalent matrix form:

$$\pmatrix{x(t)\\y(t)}=\pmatrix{x_m\\y_m}+\pmatrix{(x_s-x_m)&\varepsilon(y_s-y_m)\\(y_s-y_m)&-\varepsilon(x_s-x_m)}\pmatrix{\cos(t)\\ \sin(t)}$$

0
On

By arc do you mean a semi-circle?

If so,the given points are at either end of the diameter of a circle.

$$ (x-h)^2 + (y-k)^2 = R^2 $$

where for full circle we have:

$$ h = ( x_{start} + x_{end})/2,\,k = (y_{start} + y_{end})/2 ,\, 4 R^2 = ( x_{start} - x_{end}) ^2 + ( y_{start} - y_{end}) ^2 $$

Parametric form

$$ x= h + R \cos (t- \beta),\,y= k + R \sin (t- \beta),\, \tan \beta = \frac{y_{start} -k}{x_{start} -h} ; ( 0<t< \pi);$$

0
On

If you mean by arc any part of a circle there are an infinite number of circles going through two points. If you mean half of a circle there are TWO (for example upper half and lower half or left half and right half of a circle). So two points are still ambiguous (unless any of the two satisfies) defining a half circle.

I don't know OpenGL but in Java there is a drawArc function with input x, y defining the left uppercorner of the boundingbox and h , w defining the size of the box for the arc. For circles h = w. Then it needs the start angle (defined by a line through your points) and an extend in degrees which for half circles will be 180 or - 180 depending which half circle you want.