-
From: http://www.oschina.net/code/snippet_149523_14180
-
Describe
-
Give you three points, representing the three vertices of a triangle, now your task is to find out the area of the triangle.
-
Input
-
Each row is a set of test data, with 6 integer x1,y1,x2,y2,x3,y3 representing the horizontal ordinate of three points, respectively. (Coordinate values from 0 to 10000)
Input 0 0 0 0 0 0 indicates end of input
No more than 10000 test data sets
-
Output
-
Outputs the area of the triangle represented by these three points, resulting in a precision of 1 digits after the decimal point (even if an integer is a decimal digit)
-
Sample input
-
0 0 1 1 1 30 1 1 0 0 00 0 0 0 0 0
-
Sample output
-
1.00.5
Tags:< no > Code Snippets (1) [Full screen view all code]1. [File] 2011123001.c ~ 962B Download (25)skip to [1] [Full screen preview]?
1234567891011121314151617181920212223242526272829303132333435363738 |
/*--------------------------------
输入三个点坐标,求三角形面积
--------------------------------*/
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int x1,y1,x2,y2,x3,y3;
//各点坐标
double a,b,c;
//边长
double p,*s=NULL;
//p是周长的一半,指针s用来开辟空间储存各个三角形的面积
int count=0,i;
//count用于计数
scanf
(
"%d%d%d%d%d%d"
,&x1,&y1,&x2,&y2,&x3,&y3);
while
(x1 || x2 || x3 || y1 || y2 || y3)
{
count++;
if
(count%5==1)
s=(
double *)
realloc
(s,(count/5+1)*5*
sizeof
(
double
));
a=
sqrt
( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
b=
sqrt
( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) );
c=
sqrt
( (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) );
p=(a+b+c)/2;
*(s+count-1)=
sqrt
( p*(p-a)*(p-b)*(p-c) );
scanf
(
"%d%d%d%d%d%d"
,&x1,&y1,&x2,&y2,&x3,&y3);
}
for
(i=0;i<count;i++)
printf
(
"%.1f\n"
,*(s+i));
free
(s);
}
|
Triangular area (Helen Formula)