Skip to content

Instantly share code, notes, and snippets.

@neizod
Forked from 140bytes/LICENSE.txt
Created January 25, 2012 18:17
Show Gist options
  • Save neizod/1677702 to your computer and use it in GitHub Desktop.
Save neizod/1677702 to your computer and use it in GitHub Desktop.
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[1]-r[1]
      ^         ***___    r   |
        ^         s ._____.   v
          ^         |    /    ^
            ^       |   /     |
              ^     |  /      | b
                ^   | /       |
                  ^ |/        |
                    . q       v
<------------------> <---->
     q[0]-p[0]         a

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

. p               p".         ^
                    | \       |
                    |  \      | p[1]-r[1]
 p'               s |   \ r   |
.___________________._____.   v
^                   |    /    ^
    ^               |   /     |
        ^           |  /      | b
            ^       | /       |
                ^   |/        |
                    . q       v
<------------------> <---->
     q[0]-p[0]         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[0]-p[0]) * (r[1]-q[1]) + // square area of the large one
(r[0]-q[0]) * (p[1]-q[1]) // square area of the small one
)/2 // turn those square area into triangle area
}
function(p,q,r){return Math.abs((q[0]-p[0])*(r[1]-q[1])+(r[0]-q[0])*(p[1]-q[1]))/2}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Nattawut Phetmak <neizod@gmail.com>
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"
]
}
<!DOCTYPE html>
<title>Area of a Triangle</title>
<div>Expected value: <b>2.5, 3, 0</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var tri_area = function(p,q,r){return Math.abs((q[0]-p[0])*(r[1]-q[1])+(r[0]-q[0])*(p[1]-q[1]))/2}
document.getElementById( "ret" ).innerHTML = [tri_area([1,3],[0,0],[2,1]),
tri_area([4,7],[6,3],[4,4]),
tri_area([1,1],[4,4],[9,9])];
</script>
@neizod
Copy link
Author

neizod commented Feb 20, 2012

@maettig sorry i take so long for review this, thanks for your hack! ^___^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment