11package math ;
22
3- /**
4- * A collection of common analytical geometry.
5- *
6- * @author Simon
7- * @version 0.2, 30 May 2016
8- *
9- */
103public class GeometryUtil {
114
125 /**
@@ -29,138 +22,83 @@ public static Vector3f calculateCentroid(Vector3f a, Vector3f b,
2922 return new Vector3f (x , y , z );
3023 }
3124
32- // public static Vector2f calculateIntersection(Ray2D ray, Bounds2D bounds)
33- // {
34- // return null;
35- // }
36-
37- // public static Vector2f calculateIntersection(Ray2D ray, Bounds2D bounds)
38- // {
39- // float a = bounds.getWidth();
40- // float b = bounds.getHeight();
41- // float x0 = ray.origin.x;
42- // float y0 = ray.origin.y;
43- // float rx = bounds.getCenterX();
44- // float ry = bounds.getCenterY();
45- // float vx = ray.direction.x;
46- // float vy = ray.direction.y;
47- // float t = (a * (x0 - rx) + b * (y0 - ry)) / (a * vx + b * vy);
48- //
49- // return null;
50- // }
25+ /**
26+ * Calculates the centroid (or center of mass) of a triangle defined by
27+ * three 2D points.
28+ *
29+ * @param a The first point of the triangle.
30+ * @param b The second point of the triangle.
31+ * @param c The third point of the triangle.
32+ * @return The centroid of the triangle.
33+ */
34+ public static Vector2f getMainEmphasis (Vector2f a , Vector2f b , Vector2f c ) {
35+ Vector2f m = a .add (b ).add (c ).mult (Mathf .ONE_THIRD );
36+ return m ;
37+ }
5138
5239 /**
53- * Calculates the center of the polygon specified by the four points a, b, c
54- * and d.
40+ * Calculates the center point of a quadrilateral defined by four 2D points.
5541 *
56- * @param a the first point of the polygon
57- * @param b the second point of the polygon
58- * @param c the third point of the polygon
59- * @param d the fourth point of the polygon
60- * @return the center point of the polygon as {@link Vector2f}
42+ * @param a The first point of the quadrilateral.
43+ * @param b The second point of the quadrilateral.
44+ * @param c The third point of the quadrilateral.
45+ * @param d The fourth point of the quadrilateral.
46+ * @return The center point of the quadrilateral.
6147 */
6248 public static Vector2f calculateCenter (Vector2f a , Vector2f b , Vector2f c ,
6349 Vector2f d ) {
64- // Vector2f center = a.add(b).add(c).add(d).mult(0.25f);
65- // return center;
66- // Implemented to avoid to many object creations
6750 float x = (a .x + b .x + c .x + d .x ) * 0.25f ;
6851 float y = (a .y + b .y + c .y + d .y ) * 0.25f ;
6952 return new Vector2f (x , y );
7053 }
7154
7255 /**
73- * Calculates the center of the polygon specified by the four points a, b, c
74- * and d.
56+ * Calculates the center point of a quadrilateral defined by four 3D points.
7557 *
76- * @param a the first point of the polygon
77- * @param b the second point of the polygon
78- * @param c the third point of the polygon
79- * @param d the fourth point of the polygon
80- * @return the center point of the polygon as {@link Vector3f}
58+ * @param a The first point of the quadrilateral.
59+ * @param b The second point of the quadrilateral.
60+ * @param c The third point of the quadrilateral.
61+ * @param d The fourth point of the quadrilateral.
62+ * @return The center point of the quadrilateral.
8163 */
8264 public static Vector3f calculateCenter (Vector3f a , Vector3f b , Vector3f c ,
8365 Vector3f d ) {
84- // Vector2f center = a.add(b).add(c).add(d).mult(0.25f);
85- // return center;
86- // Implemented to avoid to many object creations
8766 float x = (a .x + b .x + c .x + d .x ) * 0.25f ;
8867 float y = (a .y + b .y + c .y + d .y ) * 0.25f ;
8968 float z = (a .z + b .z + c .z + d .z ) * 0.25f ;
9069 return new Vector3f (x , y , z );
9170 }
9271
9372 /**
94- * Calculates the point on a circle.
95- *
96- * @param center the center of the circle
97- * @param radius the radius of the circle
98- * @param angle the angle of the point to return in radians
99- * @param cw true to rotate clockwise, false to rotate counterclockwise
100- * (ccw)
101- * @return the newly created point
102- */
103- public static Vector2f pointOnCircle (Vector2f center , float radius ,
104- float angle , boolean cw ) {
105- angle = cw ? angle : -angle ;
106- float x = (float ) (center .x + radius * Math .cos (angle ));
107- float y = (float ) (center .y + radius * Math .sin (angle ));
108- return new Vector2f (x , y );
109- }
110-
111- /**
112- * Calculates the distribution point of a line segment.
73+ * Calculates the midpoint between two 3D points.
11374 *
114- * @param start the start point of the line segment
115- * @param end the end point of the line segment
116- * @param lambda the part ratio
117- * @return the the distribution point
75+ * @param start The starting point.
76+ * @param end The ending point.
77+ * @return The midpoint between the two points.
11878 */
119- public Vector2f getDistributionPoint (Vector2f start , Vector2f end ,
120- float lambda ) {
121- float scalar = 1f / (1f + lambda );
122- return start .add (end .mult (lambda )).mult (scalar );
123- }
124-
125- /**
126- * Calculates the main emphasis of a triangle.
127- *
128- * @param a the first point of the triangle
129- * @param b the second point of the triangle
130- * @param c the third point of the triangle
131- * @return the main emphasis of the triangle described by a,b,c
132- */
133- public static Vector2f getMainEmphasis (Vector2f a , Vector2f b , Vector2f c ) {
134- Vector2f m = a .add (b ).add (c ).mult (Mathf .ONE_THIRD );
135- return m ;
79+ public static Vector3f getMidpoint (Vector3f start , Vector3f end ) {
80+ return start .add (end ).mult (0.5f );
13681 }
13782
13883 /**
139- * Returns the midpoint of the line segment .
84+ * Calculates the midpoint between two 2D points .
14085 *
141- * @param start the start point of the line segment
142- * @param end the end point of the line segment
143- * @return the midpoint of the line segment
86+ * @param start The starting point.
87+ * @param end The ending point.
88+ * @return The midpoint between the two points.
14489 */
14590 public static Vector2f getMidpoint (Vector2f start , Vector2f end ) {
146- // M = 1/2 * (A + B)
147- Vector2f m = start .add (end ).mult (0.5f );
148- return m ;
91+ return start .add (end ).mult (0.5f );
14992 }
15093
15194 /**
152- * Returns the midpoint of the line segment.
153- *
154- * @param start the start point of the line segment
155- * @param end the end point of the line segment
156- * @return the midpoint of the line segment
95+ * Calculates the angle in radians between two 3D vectors.
96+ *
97+ * @param v1 The first vector.
98+ * @param v2 The second vector.
99+ * @return The angle between the two vectors in radians.
100+ * @throws IllegalArgumentException if either vector has zero length.
157101 */
158- public static Vector3f getMidpoint (Vector3f start , Vector3f end ) {
159- // M = 1/2 * (A + B)
160- Vector3f m = start .add (end ).mult (0.5f );
161- return m ;
162- }
163-
164102 public static double angleBetweenVectors (Vector3f v1 , Vector3f v2 ) {
165103 double dotProduct = v1 .dot (v2 );
166104 double magnitude1 = v1 .length ();
@@ -171,10 +109,49 @@ public static double angleBetweenVectors(Vector3f v1, Vector3f v2) {
171109 "Vectors cannot have zero length" );
172110 }
173111
174- double cosTheta = dotProduct / (magnitude1 * magnitude2 );
112+ // Handle floating-point precision issues
113+ double cosTheta = Math .max (-1.0 ,
114+ Math .min (1.0 , dotProduct / (magnitude1 * magnitude2 )));
115+
175116 double angle = Math .acos (cosTheta );
176117
177118 return angle ;
178119 }
179120
121+ /**
122+ * Calculates a point along the line segment between two 2D points.
123+ *
124+ * @param start The starting point of the line segment.
125+ * @param end The ending point of the line segment.
126+ * @param lambda A value between 0 and 1 that determines the position of the
127+ * point along the line segment. A value of 0 will return the
128+ * start point, a value of 1 will return the end point, and
129+ * values between 0 and 1 will return points in between.
130+ * @return The point along the line segment.
131+ */
132+ public Vector2f getDistributionPoint (Vector2f start , Vector2f end ,
133+ float lambda ) {
134+ float scalar = 1f / (1f + lambda );
135+ return start .add (end .mult (lambda )).mult (scalar );
136+ }
137+
138+ /**
139+ * Calculates a point on a circle given a center point, radius, angle, and
140+ * direction.
141+ *
142+ * @param center The center point of the circle.
143+ * @param radius The radius of the circle.
144+ * @param angle The angle in radians.
145+ * @param cw If true, the angle is measured clockwise from the positive
146+ * x-axis. If false, the angle is measured counterclockwise.
147+ * @return A point on the circle.
148+ */
149+ public static Vector2f pointOnCircle (Vector2f center , float radius ,
150+ float angle , boolean cw ) {
151+ angle = cw ? angle : -angle ;
152+ float x = (float ) (center .x + radius * Math .cos (angle ));
153+ float y = (float ) (center .y + radius * Math .sin (angle ));
154+ return new Vector2f (x , y );
155+ }
156+
180157}
0 commit comments