I drew this graphic:
If you check better all 4 points including the center of the circle are on Z = z1. I know the center of the circle which is (x1, y1, z1). I also know the top point which is (x1, y1 + r, z1). And I know that the other two points are on angle of 120 degrees. How can I find x2, y2, x3 and y3 using some useful functions from Unity.
Note: I know pytagorean theroem. No need to mention it.
I could possibly make some math with lines on a plane and where they intersect but that seems rather complex.
One quick dirty way on top of my head : use a Transform as a helper.
-
transform.position = circleCenter
-
transform.LookAt(topPoint, Vector3.up)
-
transform.Rotate(Vector3.up * 120f)
-
transform.TransformPoint(Vector3.forward * radius) <<
-
Repeat step 3 & 4 for the last point
Note that the Vector3.up of the second step need to represent the “up” direction of the circle.
Or you could do it with trigonometry :
x2 = x1 + cos(30°) * r;
x3 = x1 - cos(30°) * r;
y2 = y3 = y1 - sin(30°) * r;
but to make it even simpler ‘sin(30°)’ is equal to 1/2
so y2 = y3 = y1 - r/2;
In Unity :
`
float cos30_R = Mathf.Cos(30 * Mathf.Deg2Rad) * r;
x2 = x1 + cos30_R;
x3 = x1 - cos30_R;
y2 = y3 = y1 - r/2;
`
It should use less ressources then your method.
This function works perfectly:
function RotatePointAroundPivot(point: Vector3, pivot: Vector3, angles: Vector3): Vector3 {
var dir: Vector3 = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point; // return it
}
From this question: Rotate a vector around a certain point. - Questions & Answers - Unity Discussions