@@ -5,33 +5,45 @@ import './Text';
55class pSTriangle {
66 /**
77 * Creates a new Triangle
8- * @param p0 Vector to the first point of the triangle (default (0, 0))
8+ * @param p0 Vector to the first point of the triangle
99 * @param p1 Vector to the second point of the triangle (default (0, 0))
1010 * @param p2 Vector to the third point of the triangle (default (0, 0))
1111 * @param fillColor The triangle fill color (default none)
1212 * @param strokeColor The triangle stroke color (default none)
1313 * @param strokeWeight The triangle strokeWeight (default 1)
1414 */
15- constructor ( p0 = new Vector ( ) , p1 = new Vector ( ) , p2 = new Vector ( ) , fillColor = 'white' , strokeColor = 'none' , strokeWeight = 1 ) {
16- this . p0 = new Vector ( p0 . x , p0 . y ) ;
17- this . p1 = new Vector ( p1 . x , p1 . y ) ;
18- this . p2 = new Vector ( p2 . x , p2 . y ) ;
15+ constructor ( p0 , p1 = new Vector ( ) , p2 = new Vector ( ) , fillColor = 'white' , strokeColor = 'none' , strokeWeight = 1 ) {
16+ this . setCoordinates ( p0 , p1 , p2 ) ;
1917
20- //The barycenter, or center of mass. This is the
18+ // Colors
19+ this . fillColor = fillColor ;
20+ this . strokeColor = strokeColor ;
21+ this . strokeWeight = strokeWeight ;
22+ }
23+
24+
25+ /**
26+ * @param p0 Vector to the first point of the triangle
27+ * @param p1 Vector to the second point of the triangle (default last point)
28+ * @param p2 Vector to the third point of the triangle (default last point)
29+ */
30+ setCoordinates ( p0 , p1 , p2 ) {
31+ this . p0 = p0 || new Vector ( p0 . x , p0 . y ) ;
32+ this . p1 = p1 == undefined ? this . p1 : new Vector ( p1 . x , p1 . y ) ;
33+ this . p2 = p2 == undefined ? this . p2 : new Vector ( p2 . x , p2 . y ) ;
34+
35+ // The barycenter, or center of mass. This is the
2136 // point around which the triangle will rotate.
2237 this . pG = Vector . div ( Vector . add ( Vector . add ( this . p0 , this . p1 ) , this . p2 ) , 3 ) ;
2338
24- //The vectors pointing from the barycenter to P0, P1
39+ // The vectors pointing from the barycenter to P0, P1
2540 // and P2. They are useful when rotating the triangle.
2641 this . v0 = Vector . sub ( this . p0 , this . pG ) ;
2742 this . v1 = Vector . sub ( this . p1 , this . pG ) ;
2843 this . v2 = Vector . sub ( this . p2 , this . pG ) ;
29-
30- this . fillColor = fillColor ;
31- this . strokeColor = strokeColor ;
32- this . strokeWeight = strokeWeight ;
3344 }
3445
46+
3547 /**
3648 * Rotates the triangle by the specified angle, around
3749 * its center of mass.
@@ -47,12 +59,14 @@ class pSTriangle {
4759 this . p2 = Vector . add ( this . v2 , this . pG ) ;
4860 }
4961
62+
5063 /**
5164 * Updates the triangle
5265 * @param dt Delta time since last update
5366 */
5467 update ( dt ) { }
5568
69+
5670 /**
5771 * Draws the triangle on the screen.
5872 */
0 commit comments