
Klasa Triangle
Klasa opisująca trójkąt. Dziedziczy po Figure.
Konstruktor
0, 1, 3 albo 6 argumentów.
var triangle = new Triangle(). Tworzy trójkąt zerowy o współrzędnych P1(0.0, 0.0), P2(0.0, 0.0) oraz P3(0.0, 0.0).
var Triangle = new Triangle(new Array(new Point(x1,y1), new Point(x2,y2), new Point(x3, y3))). Przyjmuje tablicę zawierającą trzy punkty Point. Tworzy trójkąt między tymi punktami.
var Triangle = new Trianglet(new Point(x1,y1), new Point(x2,y2), new Point(x3, y3)). Przyjmuje trzy punkty code>Point. Tworzy trójkąt między tymi punktami.
var Traingle = new Triangle(x1, y1, x2, y2, x3, y3). . Przyjmuje sześć wspołrzędnych trzech punktów. Tworzy trzy punkty Point i trójkąt między tymi punktami.
Właściwości
| Właściwość | Opis | Wartość lub klasa obiektu | Wartość domyślna |
|---|---|---|---|
| points | tablica zawierająca trzy punkty Point tworząceten trójkąt |
Array |
[] |
Funkcje
| Typ zwracany | Nazwa | Opis | Przykład |
|---|---|---|---|
| boolean | isTriangle() |
Sprawdza czy trzy punkty tworzą trójkąt. | Rysowanie trójkąta |
| liczba rzeczywista | area() |
Oblicza i zwraca pole powierzchni tego trójkąta. | |
| liczba rzeczywista | perimeter() |
Oblicza i zwraca obwód tego trójkąta. | |
Triangle |
deepclone() |
Tworzy głęboką kopię tego trójkąta. | |
Point |
barycenter() |
Zwraca punkt Point będący środkiem (ciężkości)tego trójkąta. |
|
void |
draw(context) |
Odrysowuje ten trójkąt na kontekście contextpodanym jako argument. |
Listing
var Triangle = function() {
this.points = [];
var len = arguments.length;
if (len == 0) {
this.points[0] = new Point(0.0, 0.0);
this.points[1] = new Point(0.0, 0.0);
this.points[2] = new Point(0.0, 0.0);
} else if (len == 1) {
for (var i = 0; i <3; i++) {
this.points[i] = new Point(arguments[0][i].x, arguments[0][i].y);
}
} else if (len == 3) {
this.points[0] = new Point(arguments[0].x, arguments[0].y);
this.points[1] = new Point(arguments[1].x, arguments[1].y);
this.points[2] = new Point(arguments[2].x, arguments[2].y);
} else if (len == 6) {
this.points[0] = new Point(arguments[0], arguments[1]);
this.points[1] = new Point(arguments[2], arguments[3]);
this.points[2] = new Point(arguments[4], arguments[5]);
}
};
Triangle.prototype = new Figure();
Triangle.prototype.isTriangle = function() {
var d1 = distance(this.points[0], this.points[1]);
var d2 = distance(this.points[0], this.points[2]);
var d3 = distance(this.points[1], this.points[2]);
var tab = new Array(d1, d2, d3);
tab.sort(comparison);
if (tab[0] < tab[1] + tab[2]) {
return true;
}
return false;
};
Triangle.prototype.area = function() {
var a = distance(this.points[0], this.points[1]);
var b = distance(this.points[0], this.points[2]);
var c = distance(this.points[1], this.points[2]);
var p = (a + b + c) / 2.0;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
};
Triangle.prototype.perimeter = function() {
var a = distance(this.points[0], this.points[1]);
var b = distance(this.points[0], this.points[2]);
var c = distance(this.points[1], this.points[2]);
return a + b + c;
};
Triangle.prototype.deepclone = function() {
var figure = new Triangle();
figure.globalAlpha = this.globalAplpha;
figure.globalCompositeOperation = this.globalCompositeOperation;
figure.strokeStyle = this.strokeStyle;
figure.fillStyle = this.fillStyle;
figure.shadowOffsetX = this.shadowOffsetX;
figure.shadowOffsetY = this.shadowOffsetY;
figure.shadowBlur = this.shadowBlur;
figure.shadowColor = this.shadowColor;
figure.lineWidth = this.lineWidth;
figure.lineCap = this.lineCap;
figure.lineJoin = this.lineJoin;
figure.miterLimit = this.miterLimit;
figure.lineDashOffset = this.lineDashOffset;
figure.lineDashSegments = [];
for(var i=0; i<this.lineDashSegments.length;i++){
figure.lineDashSegments[i] = this.lineDashSegments[i];
}
figure.fill = this.fill;
figure.stroke = this.stroke;
figure.points = [];
for (var i = 0; i < this.points.length; i++) {
figure.points[i] = new Point(this.points[i].x, this.points[i].y);
}
return figure;
};
Triangle.prototype.barycenter = function() {
var x = (this.points[0].x + this.points[1].x + this.points[2].x) / 3.0;
var y = (this.points[0].y + this.points[1].y + this.points[2].y) / 3.0;
return new Point(x, y);
};
Triangle.prototype.draw = function(context) {
context.save();
context.beginPath();
this.lineCap = "square";
this.setProps(context);
context.moveTo(this.points[0].x, this.points[0].y);
context.lineTo(this.points[1].x, this.points[1].y);
context.lineTo(this.points[2].x, this.points[2].y);
context.closePath();
if (this.stroke == true) {
context.stroke();
}
if (this.fill == true) {
context.fill();
}
context.restore();
};
