Draw a equilateral triangle given the center
For your first question
Point C in your diagram above is simple, just ( cx, cy + r ).
I can think of two fairly easy ways to get the points a and b:
First Method: Pretend that (cx,cy) is the origin and rotate point C by 60 degrees and by 120 degrees in order get a and b. This can be accomplished with the formula:
- b.x = c.x * cos( 120 degrees ) - ( c.y * sin( 120 degrees ) )
- b.y = c.x * sin( 120 degrees ) + ( c.y * cos( 120 degrees ) )
- a.x = c.x * cos( 240 degrees ) - ( c.y * sin( 240 degrees ) )
- a.y = c.x * sin( 240 degrees ) + ( c.y * cos( 240 degrees ) )
Also take a look at this article on wikipedia.
Second Method: draw a line which passes through (c.x, c.y) and has a -30 degree slope. Where that line intersects with the circle will be point b. The circle is defined by the equation:
( x - c.x )^2 + ( y - c.y )^2 = r^2
(note, there will be two intersection points, so choose the right one).
Then do the same with a positive 30 degree angle line through (c.x, c.y) to get point a.
Your lines would have slopes: 1 / sqrt(3) and -1 / sqrt(3)
For your second question
Once you have the points A, B, and C that form the equilateral triangle, one of the fastest and easiest ways to detect if a point (x,y) lies in the triangle is based on the cross product and vectors.
Basically, see if (x,y) is to the left of the "vector" A->B. Then see if it is to the left of B->C. Then check to see if it is to the left of C->A.
The following method quoted from here lets you check if a point is to the left of a vector.
public bool isLeft(Point A, Point B, Point C){
return ((B.x - A.x)*(C.y - A.y) - (B.y - A.y)*( C.x - A.x)) > 0;
}
In the method A = line point1, b = line point2, and c = point to check against.
At this moment, i can only answer your second question. Just do a point-line intersection test, provided that you have stored the points which define your triangle. You can find many relative algorithms in computer graphics books.
Edit: I thought a methodology to solve your basic problem (find the 3 points which define the equilateral triangle with cx, cy and radius as given data). It relies on the property that the sum of the 3 angles of any triangle is 180 degrees. I need some time to do a further check to ensure it's correct. Then, i will edit my answer to post it.
Edit-Full Answer: Implementation of this algorithmic sketch is relying on the programming language and graphics API of your choice:
- Translate the center of the 2D coordinate system (assume it's clockwise) to the center of the centroid circle.
- Store the 2 points which defines 2 straight line segments, along with the center of the circle. The union of these segments may give you the diameter of the circle. Both segments have the center of the circle as their 1 defining point. The other 2 defining points (which you need to store) are on the circumference of the circle (use the radius length of the circle to find them).
- Rotate the 2 points onto circumference by 60 degrees, one of them clockwise and the other counterclockwise. Store their new coordinates. Draw the line that connects them. You have the 2 of the 3 triangle vertices.
- Do inverse translation of the coordinate system's center.
- The remaining vertex of the triangle is the intersection point of a circle radius and the two line segments, each of them starts from its respective known triangle vertex.
- At last, store the vertices and draw the triangle.
Hope that helps. I will try to add some drawings to clarify a bit more the steps of this methodology. Also, I will program and test these steps to ensure that solve your problem correctly.
For anyone lazy (like me) who just wants coordinates for an equilateral triangle on the unit circle:
A: (-0.866, -0.5)
B: (0.866, -0.5)
C: (0.0, 1.0)
For a different position and/or radius, multiply all values by r
, then add cx
to the x
coordinates and cy
to the y
s.