Arbitrary Triangle

https://www.matheretter.de/wiki/beliebiges-dreieck

by Kajus

HTML

<script src="https://cindyjs.org/dist/latest/Cindy.js"></script>
<script src="https://cindyjs.org/dist/v0.8.7/Cindy.js"></script>
<div id="cscanvas"></div>

JavaScript

// Find me at https://www.matheretter.de/wiki/beliebiges-dreieck

var cdy = CindyJS({
  scripts: {
    init: `
// credits https://www.mathelounge.de/642821
unit(v) := ( 
v.x/sqrt(v.x*v.x+v.y*v.y), v.y/sqrt(v.x*v.x+v.y*v.y);
);

drawwedge(p1, p2, p3, wradius, wcolor) := (
fillarc(p1+radius*unit(p2-p1), p1+radius*unit(p3+p2-2*p1), p1+radius*unit(p3-p1), color->wcolor);
fillpoly( [p1, (p1+radius*unit(p2-p1)), p1+radius*unit(p3-p1)], color->wcolor);
draw( [p1+radius*unit(p2-p1), p1+radius*unit(p3-p1)], color->wcolor, size->1.5);
);

// Variables

linea = 0;
lineb = 0;
linec = 0;

α = 50°;
β = 50°;
γ = 80°;
radius = 0.7;
anglepos = (4,4);

circumference = 0;

// colors
cblack = (0.1,0.1,0.1);
cblue = (0.1,0.1,1.0);
cred = (1.0,0.1,0.1);
cgreen = (0.1,0.7,0.1);
cbluelight = (0.4,0.4,1.0);
credlight = (1.0,0.4,0.4);
cgreenlight = (0.4,1.0,0.4);
`,
    draw: `

// calculate length of all lines via Pythagoras
linec = sqrt( pow(B.x-A.x, 2) + pow(B.y-A.y, 2) ); // lineAB
linea = sqrt( pow(C.x-B.x, 2) + pow(C.y-B.y, 2) ); // lineBC
lineb = sqrt( pow(C.x-A.x, 2) + pow(C.y-A.y, 2) ); // lineAC
// print(lineAB+" / "+lineBC+" / "+lineAC);

circumference = linea + lineb + linec;

// calculate angles via Kosinussatz
α = arccos( (-pow(linea,2) + pow(lineb,2) + pow(linec,2)) / ( 2*lineb*linec ) );
β = arccos( (-pow(lineb,2) + pow(linea,2) + pow(linec,2)) / ( 2*linea*linec ) );
γ = arccos( (-pow(linec,2) + pow(linea,2) + pow(lineb,2)) / ( 2*linea*lineb ) );

// calculate area
area = (A.x * B.y - B.x * A.y) + (B.x * C.y - C.x * B.y) + (C.x * A.y - A.x * C.y);
area = |area|;

// calculate centroid
centroid = [ (A.x+B.x+C.x)/3, (A.y+B.y+C.y)/3];


// drawpoly( [A,B,C], color->(0,0,1), size->2);
// fillpoly( [A,B,C], color->(0,0,1), alpha->0.5);

// draw angles in triangle
// lineABc = sqrt( pow(B.x-A.x,2) + pow(A.y-B.y,2) );
// startα = arccos( (-pow(A.y-B.y,2) + pow(B.x-A.x,2) + pow(lineABc,2)) / ( 2*(B.x-A.x)*lineABc ) );
startα = 0;
startβ = 0;
startγ = 0;


// anglemark(A, -startα,...