Skip to main content
rule change
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 1818 16 bytes

T2/T(f.-~(+.*:-z.5*
T                  e# Push T.
                   e# [a b c d e f]
T2/                e# Split T into pairs.
                   e# [[a b] [c d] [e f]]
   (               e# Shift out the first pair.
                   e# [[c d] [e f]] [a b]
    f.-            e# For [c d] and [e f], perform vectorized
                   e# subtraction with [a b].
                   e# [[c-a d-b] [e-a f-b]]
       ~           e# Dump the array on the stack.
                   e# [c-a d-b] [e-a f-b]
        (+         e# Shift and append. Rotates the second array.
                   e# [c-a d-b] [f-b e-a]
          .*       e# Vectorized product.
                   e# [(c-a)(f-b) (d-b)(e-a)]
            :-     e# Reduce by subtraction.
                   e# (c-a)(f-b) - (d-b)(e-a)
              z    e# Apply absolute value.
                   e# |(c-a)(f-b) - (d-b)(e-a)|
               .5* e# Multiply by 0.5.
                   e# |(c-a)(f-b) - (d-b)(e-a)| / 2

[1]: http://cjam.aditsu.net/#code=q~%3AT%3B%20e%23%20Store%20input%20in%20T.%0A%0AT2%2F%0A%0AT(f.-~(%2B.%3A-z.5&input=%5B1%202%204%202%203%207%5D&input=%5B%5B1%202%5D%20%5B4%202%5D%20%5B3%207%5D%5D [2]: https://en.wikipedia.org/wiki/Triangle#Using_coordinates "Triangle - Wikipedia, the free encyclopedia"

CJam, 18 bytes

T2/(f.-~(+.*:-z.5*
T                  e# Push T.
                   e# [a b c d e f]
T2/                e# Split T into pairs.
                   e# [[a b] [c d] [e f]]
   (               e# Shift out the first pair.
                   e# [[c d] [e f]] [a b]
    f.-            e# For [c d] and [e f], perform vectorized
                   e# subtraction with [a b].
                   e# [[c-a d-b] [e-a f-b]]
       ~           e# Dump the array on the stack.
                   e# [c-a d-b] [e-a f-b]
        (+         e# Shift and append. Rotates the second array.
                   e# [c-a d-b] [f-b e-a]
          .*       e# Vectorized product.
                   e# [(c-a)(f-b) (d-b)(e-a)]
            :-     e# Reduce by subtraction.
                   e# (c-a)(f-b) - (d-b)(e-a)
              z    e# Apply absolute value.
                   e# |(c-a)(f-b) - (d-b)(e-a)|
               .5* e# Multiply by 0.5.
                   e# |(c-a)(f-b) - (d-b)(e-a)| / 2

[1]: http://cjam.aditsu.net/#code=q~%3AT%3B%20e%23%20Store%20input%20in%20T.%0A%0AT2%2F(f.-~(%2B.%3A-z.5&input=%5B1%202%204%202%203%207%5D [2]: https://en.wikipedia.org/wiki/Triangle#Using_coordinates "Triangle - Wikipedia, the free encyclopedia"

CJam, 18 16 bytes

T(f.-~(+.*:-z.5*
T                  e# Push T.
                   e# [[a b] [c d] [e f]]
   (               e# Shift out the first pair.
                   e# [[c d] [e f]] [a b]
    f.-            e# For [c d] and [e f], perform vectorized
                   e# subtraction with [a b].
                   e# [[c-a d-b] [e-a f-b]]
       ~           e# Dump the array on the stack.
                   e# [c-a d-b] [e-a f-b]
        (+         e# Shift and append. Rotates the second array.
                   e# [c-a d-b] [f-b e-a]
          .*       e# Vectorized product.
                   e# [(c-a)(f-b) (d-b)(e-a)]
            :-     e# Reduce by subtraction.
                   e# (c-a)(f-b) - (d-b)(e-a)
              z    e# Apply absolute value.
                   e# |(c-a)(f-b) - (d-b)(e-a)|
               .5* e# Multiply by 0.5.
                   e# |(c-a)(f-b) - (d-b)(e-a)| / 2

[1]: http://cjam.aditsu.net/#code=q~%3AT%3B%20e%23%20Store%20input%20in%20T.%0A%0AT(f.-~(%2B.%3A-z.5&input=%5B%5B1%202%5D%20%5B4%202%5D%20%5B3%207%5D%5D [2]: https://en.wikipedia.org/wiki/Triangle#Using_coordinates "Triangle - Wikipedia, the free encyclopedia"

added 4 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

As mentioned on [Wikipedia][2], the area of the triangle [[0 0] [x y] [z w]] can be calculated as |det([[x y] [z w]])| / 2 = |xw-yz| / 2.

As mentioned on [Wikipedia][2], the area of the triangle [[0 0] [x y] [z w]] can be calculated as |det([[x y] [z w]])|/2 = |xw-yz|/2.

As mentioned on [Wikipedia][2], the area of the triangle [[0 0] [x y] [z w]] can be calculated as |det([[x y] [z w]])| / 2 = |xw-yz| / 2.

added 1558 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

[TryTry it online in the [CJam interpreter][1].][1]

Idea

As mentioned on [Wikipedia][2], the area of the triangle [[0 0] [x y] [z w]] can be calculated as |det([[x y] [z w]])|/2 = |xw-yz|/2.

For a generic triangle [[a b] [c d] [e f]], we can translate its first vertex to the origin, thus obtaining the triangle [[0 0] [c-a d-b] [e-a f-b]], whose area can be calculated by the above formula.

Code

T                  e# Push T.
                   e# [a b c d e f]
T2/                e# Split T into pairs.
                   e# [[a b] [c d] [e f]]
   (               e# Shift out the first pair.
                   e# [[c d] [e f]] [a b]
    f.-            e# For [c d] and [e f], perform vectorized
                   e# subtraction with [a b].
                   e# [[c-a d-b] [e-a f-b]]
       ~           e# Dump the array on the stack.
                   e# [c-a d-b] [e-a f-b]
        (+         e# Shift and append. Rotates the second array.
                   e# [c-a d-b] [f-b e-a]
          .*       e# Vectorized product.
                   e# [(c-a)(f-b) (d-b)(e-a)]
            :-     e# Reduce by subtraction.
                   e# (c-a)(f-b) - (d-b)(e-a)
              z    e# Apply absolute value.
                   e# |(c-a)(f-b) - (d-b)(e-a)|
               .5* e# Multiply by 0.5.
                   e# |(c-a)(f-b) - (d-b)(e-a)| / 2

[1]: http://cjam.aditsu.net/#code=q~%3AT%3B%20e%23%20Store%20input%20in%20T.%0A%0AT2%2F(f.-~(%2B.%3A-z.5&input=%5B1%202%204%202%203%207%5D [2]: https://en.wikipedia.org/wiki/Triangle#Using_coordinates "Triangle - Wikipedia, the free encyclopedia"

[Try it online.][1]

[1]: http://cjam.aditsu.net/#code=q~%3AT%3B%20e%23%20Store%20input%20in%20T.%0A%0AT2%2F(f.-~(%2B.%3A-z.5&input=%5B1%202%204%202%203%207%5D

Try it online in the [CJam interpreter][1].

Idea

As mentioned on [Wikipedia][2], the area of the triangle [[0 0] [x y] [z w]] can be calculated as |det([[x y] [z w]])|/2 = |xw-yz|/2.

For a generic triangle [[a b] [c d] [e f]], we can translate its first vertex to the origin, thus obtaining the triangle [[0 0] [c-a d-b] [e-a f-b]], whose area can be calculated by the above formula.

Code

T                  e# Push T.
                   e# [a b c d e f]
T2/                e# Split T into pairs.
                   e# [[a b] [c d] [e f]]
   (               e# Shift out the first pair.
                   e# [[c d] [e f]] [a b]
    f.-            e# For [c d] and [e f], perform vectorized
                   e# subtraction with [a b].
                   e# [[c-a d-b] [e-a f-b]]
       ~           e# Dump the array on the stack.
                   e# [c-a d-b] [e-a f-b]
        (+         e# Shift and append. Rotates the second array.
                   e# [c-a d-b] [f-b e-a]
          .*       e# Vectorized product.
                   e# [(c-a)(f-b) (d-b)(e-a)]
            :-     e# Reduce by subtraction.
                   e# (c-a)(f-b) - (d-b)(e-a)
              z    e# Apply absolute value.
                   e# |(c-a)(f-b) - (d-b)(e-a)|
               .5* e# Multiply by 0.5.
                   e# |(c-a)(f-b) - (d-b)(e-a)| / 2

[1]: http://cjam.aditsu.net/#code=q~%3AT%3B%20e%23%20Store%20input%20in%20T.%0A%0AT2%2F(f.-~(%2B.%3A-z.5&input=%5B1%202%204%202%203%207%5D [2]: https://en.wikipedia.org/wiki/Triangle#Using_coordinates "Triangle - Wikipedia, the free encyclopedia"

Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading