The task is as follows: 6 numbers are entered - coordinates of three vertices of the triangle. It is necessary to find its area. In the testing system, this code fails 1 test. In what cant I do not know. Python 3.3

Input data:

Six numbers - the coordinates of the three vertices of the triangle.

Output:

One number - the size of the area of ​​the triangle.

Examples

input data:

1 1 2 4 3 2

output:

2.50000

As for the accuracy of calculations and rounding, the condition says nothing, tried differently, did not help.

import math x1, y1, x2, y2, x3, y3 = list(map(int, input().split())) a = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) b = math.sqrt((x3 - x1) ** 2 + (y3 - y1) ** 2) c = math.sqrt((x2 - x3) ** 2 + (y2 - y3) ** 2) p = (a + b + c) / 2 res = math.sqrt(p * (p - a) * (p - b) * (p - c)) print(res) 
  • one
    Test example, I / O features, and calculation accuracy check. - pavel
  • one
    Add input and result to question - Igor
  • some kind of formula is too complicated ... I think you can do without roots and squares ... - MaxU
  • res = [(x2-x1) (y3-y1) - (y2-y1) (x3-x1)] / 2 - Igor
  • A = abs( x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2) ) / 2.0 - MaxU

2 answers 2

Try a different formula :

 x1, y1, x2, y2, x3, y3 = map(int, input().split()) A = abs( x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2) ) / 2.0 print(A) 

Test:

 In [22]: x1, y1, x2, y2, x3, y3 = list(map(int, input().split())) 1 1 2 4 3 2 In [23]: A = abs( x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2) ) / 2.0 In [24]: print(A) 2.5 

PS In this answer there are other formulas for finding the area of ​​a triangle for given coordinates of the vertices ( thanks to @jfs )

  • It would be nice to write where she is, so beautiful, is taken. - Igor
  • @Igor, the first link in google: google.com/… ;-) - MaxU
  • aaa from Google :). I was hoping for a "vector product". - Igor
  • one
    Other formulas are given - jfs
  • @jfs, thanks, added in response. - MaxU

If you have two vectors V1 (x1, y1) and V2 (x2, y2), then

S parallelogram = x1 * y2 - x2 * y1

will give you the signed area of ​​a parallelogram built on these vectors. (The sign of this value will determine the direction of sweeping the plane from the vector V1 to the vector V2 - clockwise or counterclockwise.) And if you divide this area by 2, then obviously the sign area of ​​the triangle formed by these vectors will be obtained.

Thus, if in your case there are coordinates of the vertices A (xa, ya), B (xb, yb) and C (xc, yc), you can choose AB for V1 and AC for V2 and get

S triangle = | (xb - xa) * (yc - ya) - (xc - xa) * (yb - ya) | / 2

The choice of sides for vectors in this case is arbitrary and the result does not depend on it.

On your input:

S triangle = | (2 - 1) * (2 - 1) - (3 - 1) * (4 - 1) | / 2 = 5/2 = 2.5

It is easy to see that this formula is also a special case of the general Gauss formula (aka "lacing formula") for the area of an arbitrary polygon. More precisely, rather, the derivation of the Gauss formula is based on this formula for the sign area of ​​the “elementary” triangle.