Instantly share code, notes, and snippets. neizod/LICENSE.txt forked from 140bytes/LICENSE.txt Created Jan 25, 2012

Area of a Triangle

Area of a Triangle

83b. Find area between 3 points which form a triangle.

How to use

Invoke function using 3 arguments of pair of coordination.

Nth arg (array) .... pair of coordination e.g. [1, 2]

Concept

from original triangle pqr:

. p                           ^
^ ***___                    |
^     ***___              | p-r
^         ***___    r   |
^         s ._____.   v
^         |    /    ^
^       |   /     |
^     |  /      | b
^   | /       |
^ |/        |
. q       v
<------------------> <---->
q-p         a

cut form p to s, and transform it into this:

. p               p".         ^
| \       |
|  \      | p-r
p'               s |   \ r   |
.___________________._____.   v
^                   |    /    ^
^               |   /     |
^           |  /      | b
^       | /       |
^   |/        |
. q       v
<------------------> <---->
q-p         a

now, there are three triangle (qsr, qsp', rsp"); or, for this hack, two triangle (large: rqp', small: rsp"), which calculate much easier!

Credit

@maettig 95b -> 93b -> 83b

 function( p, // 1st coordinate pair q, // 2nd r // 3rd ) { return Math.abs( // make sure the final area is positive (q-p) * (r-q) + // square area of the large one (r-q) * (p-q) // square area of the small one )/2 // turn those square area into triangle area }
 function(p,q,r){return Math.abs((q-p)*(r-q)+(r-q)*(p-q))/2}
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2012 Nattawut Phetmak Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.
 { "name": "areaTriangle", "description": "this will calculate the area of triangle.", "keywords": [ "area", "triangle", "math" ] }
 Area of a Triangle
Expected value: 2.5, 3, 0
Actual value:

tsaniel commented Jan 26, 2012

 Interesting Maths, and it seems it cannot be shortened any more (maybe Maths trick can). But I don't quite understand how to transform from the original triangle into the three triangles, can you explain more?
Owner Author

neizod commented Jan 26, 2012

 ok, i'll try this :D from maths: triangle area is 1/2 (width * height), where the height must be perpendicular to the width. so no matter how lean the triangle is, the area of triangle which share the same width and height will be equal. e.g. this 3 triangles area is equal since there share the same width and height: . . . ^ /| ^/ / \ | / | ^ / * * | / | ^ / / \ | h / | ^ / * * | / | ^ / / \ | ._____| ._____/ ._____. v <-----> <-----> <-----> w w w mark new point "s", which is locate at the junction of vertical line of point q, and horizontal line of point r. mark a line from point p to point s and cut it. new point p which lay on 2 side of the cut line will be named point p' and p". (actually, there is no need to done this step) to see this clearly, move point p' and p" to make right triangles. now we got 3 right triangles, which we perfectly known its width and height. so the calculation is not that hard. ;)

tsaniel commented Jan 26, 2012

 I get it :D. Grate tutorial, @neizod (and sorry for my bad Maths). Actually, the word "transform" confused me.

maettig commented Jan 27, 2012

 Here is how we calculated this in school. It's longer (107 bytes) but much easier to understand. function(a,b,c){return Math.abs((a-b)*(a+b)+(b-c)*(b+c)+(c-a)*(c+a))/2} While playing around with your version I got this (93 bytes): function(a,b,c){return Math.abs((a-c)*(c-=b)+c*(c=c-b)+(b-a)*c)/2}
Owner Author

neizod commented Jan 27, 2012

 @maettig i'm in shock..

maettig commented Jan 30, 2012

 83 bytes by applying simple math. ;-) function(a,b,c){return Math.abs((b-a)*(c-b)+(c-b)*(a-b))/2}

tsaniel commented Jan 30, 2012

 @maettig: It's insane. (ok my maths sucks). By the way, how is your dumpGlobalLeaks going?

maettig commented Feb 2, 2012

 Calculate the area of a polygon in 104 bytes. Bonus: SVG visualization.
Owner Author

neizod commented Feb 20, 2012

 @maettig sorry i take so long for review this, thanks for your hack! ^___^
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.