Skip to content

Commit 530d3cb

Browse files
committed
Changed triangle functions and added triangle example.
1 parent e28e47a commit 530d3cb

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

dev/src/Plot.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Plot {
22
constructor() {
3-
this.tex = new pSText('\\sum \\sqrt{x^2+1} + 6 x \\text{ with $x \\in \\mathbb{R}$}', new Vector(), 7, 'white');
3+
this.tr = new pSTriangle(new Vector(-2, -2), new Vector(2, -2), new Vector(0, 2), 'white');
44
}
55

66
update(dt) {
7-
7+
this.tr.rotate(0.01);
88
}
99

1010
draw(drawer) {
11-
this.tex.draw();
11+
this.tr.draw();
1212
}
1313
}

src/drawer/objects/Triangle.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,45 @@ import './Text';
55
class 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

Comments
 (0)