area of triangle

Advanced programming training

Problem Description

Given the coordinates of the three vertices of a triangle, find the area of ​​the triangle.

input form

There are multiple sets of test data.

Each set of test data occupies one line, 6 floating-point numbers separated by spaces: x1, y1, x2, y2, x3, y3. Represents the coordinates of the three vertices of the triangle.

A line of 6 0s (in the form of 0 0 0 0 0 0) indicates that the input is over and no processing is required.

40% of the vertex coordinates -10 ≤ xi, yi≤ 10; i=1,2,3

30% of the vertex coordinates -100 ≤ xi, yi≤ 100; i=1,2,3

20% of the vertex coordinates -1000 ≤ xi, yi≤ 1000; i=1,2,3

10% of the vertex coordinates -10000 ≤ xi, yi≤ 10000; i=1,2,3

output form

For each set of test data, output the area of ​​the corresponding triangle, retaining 6 decimal places.

sample

【Sample input】

1 2 3 4 -2 8
0 0 0 1 1 0
0 0 0 0 0 0
【Sample output】

9.000000
0.500000
Tips: If you use floating point numbers, please pay attention to precision issues, it is recommended to use double

Idea analysis

Very simple question, just need to be able to remember the formula

code display

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	double x1,y1,x2,y2,x3,y3;
	while(1)
	{
    
    
		cin>>x1>>y1>>x2>>y2>>x3>>y3;
		if(x1==0&&x2==0&&x3==0&&y1==0&&y2==0&&y3==0)
		return 0;
		else
		{
    
    
			double area=(x1*y2+x2*y3+x3*y1-x2*y1-x3*y2-x1*y3)/2.0;
			if(area<0)
			area=-1*area;
			cout<<fixed<<setprecision(6)<<area<<endl;
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_51295681/article/details/118519976