Centres - returns centres of a 2d triangle
Posted: 28 Dec 2013, 07:59
Centres is a little function that returns Incentre, Orthocentre, Centroid or Circumcentre of a given 2d triangle.
It can also return Euler line's equation for a triangle.
PERMANENTLY STORED AT-
GITHUB
Raw Direct Download link
It can also return Euler line's equation for a triangle.
EXAMPLEsomeone @ stack overflow wrote:The incenter is the intersection of the angle bisectors of the triangle. The centroid is the intersection of the lines from each vertex of the triangle to the middle of its opposite side. The circumcenter is the intersection of the perpendicular bisectors of the sides. The orthocenter is the intersection of the altitudes of the triangle.
Euler later proved that the centroid, circumcenter and orthocenter are collinear in any triangle. The line that these three points are on in a triangle is called the Euler Line. It is defined for every triangle except an equilateral triangle, where all the points coincide.
Code: Select all
;p:=centres([[4, 0], [-2, 4], [0, 6]], "o")
;msgbox % "Orthocentres - `n x=" p.1 "`n y=" p.2
;return
;-------------------- Function ----------------------
; r := i = incentres
; m = centroid
; c = circumcentre
; o = orthocentre
; leave r blank to return eqn of euler line
centres(z, r:=""){
mx := (z.1.1+z.2.1+z.3.1)/3 , my := (z.1.2+z.2.2+z.3.2)/3 ;medians done
s := ( c := sqrt( (z.2.1 - z.1.1)**2 + (z.2.2-z.1.2)**2 ) ) + ( a := sqrt( (z.3.1-z.2.1)**2 + (z.3.2-z.2.2)**2 ) )
+ ( b := sqrt( (z.3.1-z.1.1)**2 + (z.3.2-z.1.2)**2 ) )
ix := (a*z.1.1+b*z.2.1+c*z.3.1)/s , iy := (a*z.1.2+b*z.2.2+c*z.3.2)/s ; incentres done
midx_a := (z.3.1+z.2.1)/2 , midy_a := (z.3.2+z.2.2)/2 , slp_a := -1*(z.3.1-z.2.1)/(z.3.2-z.2.2) ;perpendicular slope
cc_a := midy_a-(slp_a*midx_a) ; b = y - mx
midx_b := (z.3.1+z.1.1)/2 , midy_b := (z.3.2+z.1.2)/2 , slp_b := -1*(z.3.1-z.1.1)/(z.3.2-z.1.2)
cc_b := midy_b-(slp_b*midx_b) ; b = y - mx
cx := (cc_b-cc_a)/(slp_a-slp_b) , cy := cc_a + (slp_a*cx) ; y := b+mx --- circumcentres done
oc_a := z.1.2-(slp_a*z.1.1) ; b = y - mx
oc_b := z.2.2-(slp_b*z.2.1)
ox := (oc_a-oc_b)/(slp_b-slp_a) , oy := oc_a + (slp_a*ox) ; orthocentres done
if r in m,i,c,o
return [Round(%r%x), Round(%r%y)]
else return "y=" Round(m:=(oy-cy)/(ox-cx)) "x+" Round(oy-m*ox)
}
PERMANENTLY STORED AT-
GITHUB
Raw Direct Download link