Centres - returns centres of a 2d triangle

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Avi
Posts: 193
Joined: 30 Sep 2013, 09:51
Location: India
Contact:

Centres - returns centres of a 2d triangle

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.
someone @ 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.
EXAMPLE

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
Writes at Dev Letters

Clipjump Clipboard Manager : More Scripts (updated 2019)

Image
Morpheus
Posts: 119
Joined: 04 Oct 2013, 05:09

Re: Centres - returns centres of a 2d triangle

28 Dec 2013, 10:34

Just out of curiosity, assuming that mass is distributed equally across the surface of the triangle, which of the above centers (If any) would be the center of gravity?
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: Centres - returns centres of a 2d triangle

28 Dec 2013, 12:33

I'm very sure it's the centroid...
"Nothing is quieter than a loaded gun." - Heinrich Heine
User avatar
Avi
Posts: 193
Joined: 30 Sep 2013, 09:51
Location: India
Contact:

Re: Centres - returns centres of a 2d triangle

29 Dec 2013, 08:49

It's not orthocentre and circumcentre as they are present at a vertex and side respectively in a right-angled triangle.
I am confused between incentre and centroid as both look to be candidates for C.O.M location when tested with few simple triangles (equilateral, isoceles, right-angled) . There should be proof with integration.

@Alibaba_ any reason why you are sure ?
Writes at Dev Letters

Clipjump Clipboard Manager : More Scripts (updated 2019)

Image
strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

Re: Centres - returns centres of a 2d triangle

29 Dec 2013, 13:20

One can parametrize
f(t, s) := a+(b-a)t+(c-b)s , for 0 <= t <= 1 and 0 <= s <= t
where a, b, c are the vertices of the triangle. If the triangle has nonvanishing 2 dimensional Volume, i.e. det(b-a, c-b) != 0, one can use f(t, s) to integrate over the triangle.
(1/Volume)*\int_0^1 dt \int_0^t ds |det(Df)|*f(t, s) == (a+b+c)/3
which is the center of mass (in case it is distributed as Morpheus said).

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 99 guests