get_triangle_centroid
get_triangle_centroid(x1, y1, x2, y2, x3, y3) -> LIST (gx,gy, mx1,my1, mx2,my2, mx3,my3)
Returns the centroid of the triangle (intersection of medians), and the coordinates of the 3 feet of the medians (midpoints of sides), in flat list
Syntaxe
var pts = get_triangle_centroid(x1=200,y1=200, x2=-200,y2=100, x3=200,y3=-200);
Arguments
| Name | Required | Default | Description |
|---|---|---|---|
x1 | yes | - | x-coordinate of the first point |
y1 | yes | - | y-coordinate of the first point |
x2 | yes | - | x-coordinate of the second point |
y2 | yes | - | y-coordinate of the second point |
x3 | yes | - | x-coordinate of the third point |
y3 | yes | - | y-coordinate of the third point |
Full example
canvas(1200, 1200, "#020410",center=true);
var quarter = width() / 4 ;
fill("");
weight(5);
var col_triangle = "#FFC107";
var col_t = col_triangle + "AA";
var col_circum = "#2196F3";
var col_incircle = "#9575CD";
var col_centroid = "#CDDC39";
var col_ortho = "#E91E63";
var x1 = 200;
var y1 = 200;
var x2 = -200;
var y2 = 100;
var x3 = 200;
var y3 = -200;
var circum = get_triangle_circumcenter(x1, y1, x2, y2, x3, y3);
var ortho = get_triangle_orthocenter(x1, y1, x2, y2, x3, y3);
var incircle = get_triangle_incircle(x1, y1, x2, y2, x3, y3);
var centroid = get_triangle_centroid(x1, y1, x2, y2, x3, y3);
var rad_big = 11;
load_text_default(30);
def drawTriangle(x1, y1, x2, y2, x3, y3, color) {
triangle(x1, y1, x2, y2, x3, y3, stroke=color, weight=3);
var cx = centroid[0];
var cy = centroid[1];
var d = 14;
var dx1 = x1 - cx;
var dy1 = y1 - cy;
var len1 = sqrt(dx1*dx1 + dy1*dy1);
var ax = x1 + d * dx1 / len1;
var ay = y1 + d * dy1 / len1;
var dx2 = x2 - cx;
var dy2 = y2 - cy;
var len2 = sqrt(dx2*dx2 + dy2*dy2);
var bx = x2 + d * dx2 / len2;
var by = y2 + d * dy2 / len2;
var dx3 = x3 - cx;
var dy3 = y3 - cy;
var len3 = sqrt(dx3*dx3 + dy3*dy3);
var cx_out = x3 + d * dx3 / len3;
var cy_out = y3 + d * dy3 / len3;
text("A", ax, ay, axy=middle, stroke=col_triangle);
text("B", bx, by, axy=middle, stroke=col_triangle);
text("C", cx_out, cy_out, axy=middle, stroke=col_triangle);
}
// TOP LEFT : CIRCUM
coord_open();
coord_translate(-quarter,-quarter);
stroke(col_t);
drawTriangle(x1, y1, x2, y2, x3, y3, col_t);
circle(circum[0], circum[1], circum[2], stroke=col_circum);
circle(circum[0], circum[1], rad_big, stroke=col_circum);
line(x1, y1, circum[9], circum[10], stroke=col_circum, weight=1);
line(x2, y2, circum[11], circum[12], stroke=col_circum, weight=1);
line(x3, y3, circum[13], circum[14], stroke=col_circum, weight=1);
circle(circum[3], circum[4], 4, stroke=col_circum);
circle(circum[5], circum[6], 4, stroke=col_circum);
circle(circum[7], circum[8], 4, stroke=col_circum);
circle(circum[9], circum[10], 4, stroke=col_circum);
circle(circum[11], circum[12], 4, stroke=col_circum);
circle(circum[13], circum[14], 4, stroke=col_circum);
text("A'", circum[3], circum[4]-30, axy=middle, stroke=col_circum);
text("A''", circum[9], circum[10]-30, axy=middle, stroke=col_circum);
// circle(circum[11], circum[12], 4, stroke=col_circum);
// circle(circum[13], circum[14], 4, stroke=col_circum);
coord_close();
// TOP RIGHT
coord_open();
coord_translate(quarter,-quarter);
stroke(col_t);
drawTriangle(x1, y1, x2, y2, x3, y3, col_t);
circle(incircle[0], incircle[1], incircle[2], stroke=col_incircle);
circle(incircle[0], incircle[1], rad_big, stroke=col_incircle);
circle(incircle[3], incircle[4], 4, stroke=col_incircle);
circle(incircle[5], incircle[6], 4, stroke=col_incircle);
circle(incircle[7], incircle[8], 4, stroke=col_incircle);
line(x1, y1, incircle[0], incircle[1], stroke=col_incircle, weight=1);
line(x2, y2, incircle[0], incircle[1], stroke=col_incircle, weight=1);
line(x3, y3, incircle[0], incircle[1], stroke=col_incircle, weight=1);
circle(incircle[9], incircle[10], 4, stroke=col_incircle);
circle(incircle[11], incircle[12], 4, stroke=col_incircle);
circle(incircle[13], incircle[14], 4, stroke=col_incircle);
text("A'", incircle[3], incircle[4]-30, axy=middle, stroke=col_incircle);
text("A''", incircle[9], incircle[10]-30, axy=middle, stroke=col_incircle);
coord_close();
// BOTTOM RIGHT
coord_open();
coord_translate(quarter,quarter);
stroke(col_t);
drawTriangle(x1, y1, x2, y2, x3, y3, col_t);
circle(circum[0], circum[1], circum[2], stroke=col_circum);
circle(ortho[0], ortho[1], rad_big, stroke=col_ortho);
circle(ortho[2], ortho[3], 2, stroke=col_ortho);
circle(ortho[4], ortho[5], 2, stroke=col_ortho);
circle(ortho[6], ortho[7], 2, stroke=col_ortho);
circle(ortho[8], ortho[9], 4, stroke=col_ortho);
circle(ortho[10], ortho[11], 4, stroke=col_ortho);
circle(ortho[12], ortho[13], 4, stroke=col_ortho);
line(x1, y1, ortho[8], ortho[9], stroke=col_ortho, weight=1);
line(x2, y2, ortho[10], ortho[11], stroke=col_ortho, weight=1);
line(x3, y3, ortho[12], ortho[13], stroke=col_ortho, weight=1);
text("A'", ortho[2], ortho[3]-30, axy=middle, stroke=col_ortho);
text("A''", ortho[8], ortho[9]-30, axy=middle, stroke=col_ortho);
coord_close();
// BOTTOM LEFT
coord_open();
coord_translate(-quarter,quarter);
stroke(col_t);
drawTriangle(x1, y1, x2, y2, x3, y3, col_t);
circle(centroid[0], centroid[1], rad_big, stroke=col_centroid);
circle(centroid[2], centroid[3], 2, stroke=col_centroid);
circle(centroid[4], centroid[5], 2, stroke=col_centroid);
circle(centroid[6], centroid[7], 2, stroke=col_centroid);
line(x1, y1, centroid[2], centroid[3], stroke=col_centroid, weight=1);
line(x2, y2, centroid[4], centroid[5], stroke=col_centroid, weight=1);
line(x3, y3, centroid[6], centroid[7], stroke=col_centroid, weight=1);
text("A'", centroid[2], centroid[3]-30, axy=middle, stroke=col_centroid);
coord_close();
///
///
///
///
load_text_default(30);
var x = 0;
var y = -50;
var step = 30;
text("circumcenter", x, y, stroke=col_circum , axy=middle); y += step;
text("incircle", x, y, stroke=col_incircle, axy=middle); y += step;
text("centroid", x, y, stroke=col_centroid, axy=middle); y += step;
text("orthocenter", x, y, stroke=col_ortho , axy=middle); y += step;
console("triangle", x1, y1, x2, y2, x3, y3);
console("circumcenter", circum);
console("incircle", incircle);
console("orthocenter", ortho);
save(whoami() + ".png");
