-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon.cpp
executable file
·54 lines (46 loc) · 1.49 KB
/
polygon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream.h>
#include<conio.h>
//from http://www.scootcity.com/cpp/polygon.htm
struct point{
float x,y;
point *p;
};
float calc_polygon(point *, point *);
float calc_triangle(point *, point *, point *);
inline float calc_determinant(float px, float py, float qx, float qy){
return(px*qy - qx*py);
}
main(){
point *head=new point;
point *current=new point;
current=head;
int i = 1;
cout << "\nEnter point " << i << ": ";
while (cin >> current->x >> current->y) {
current->p=new point;
current=current->p;
i++;
cout << "\nEnter point " << i << ": ";
}
current->p=NULL; // put 'tail' on linked list of points.
float area=calc_polygon(head, head->p);
cout << "\nArea is: " << fabs(area) << endl;
getch();
}
float calc_polygon(point *head, point *next){
float area;
if(next->p->p!=NULL){
area=calc_triangle(head, next, next->p);
next=next->p;
area+=calc_polygon(head, next);
return area;
}
return 0;
}
float calc_triangle(point *a, point *b, point *c){
float det_bc=calc_determinant(b->x, b->y, c->x, c->y);
float det_ac=calc_determinant(a->x, a->y, c->x, c->y);
float det_ab=calc_determinant(a->x, a->y, b->x, b->y);
float area=(det_bc-det_ac+det_ab)/2; // formula for area of triangle
return area;
}