-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.cpp
More file actions
68 lines (62 loc) · 2.21 KB
/
geometry.cpp
File metadata and controls
68 lines (62 loc) · 2.21 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "functions.h"
/**
* @getDistance - Returns the perimeter of a triangle given the coordinates
* @x1 - x coordinate of the first vertex
* @y1 - y coordinate of the first vertex
* @x2 - x coordinate of the second vertex
* @y2 - y coordinate of the second vertex
*/
float getDistance(float x1, float y1, float x2, float y2)
{
return (sqrt(pow((x1 - x2), 2) - pow((y1 - y2), 2)));
}
/**
* @getPerimeter - Returns the perimeter of a triangle given the coordinates
* @x1 - x coordinate of the first vertex
* @y1 - y coordinate of the first vertex
* @x2 - x coordinate of the second vertex
* @y2 - y coordinate of the second vertex
* @x3 - x coordinate of the third vertex
* @y3 - y coordinate of the third vertex
*/
float getPerimeter(float x1, float y1, float x2, float y2, float x3, float y3)
{
float AB = getDistance(x1, y1, x2, y2);
float AC = getDistance(x1, y1, x3, y3);
float BC = getDistance(x2, y2, x3, y3);
return (AB + AC + BC);
}
/**
* @getArea - Returns the area of the triangle given the coordinates of
* the three vertices
* @x1 - x coordinate of the first vertex
* @y1 - y coordinate of the first vertex
* @x2 - x coordinate of the second vertex
* @y2 - y coordinate of the second vertex
* @x3 - x coordinate of the third vertex
* @y3 - y coordinate of the first vertex
*/
float getArea(float x1, float y1, float x2, float y2, float x3, float y3)
{
float AB = getDistance(x1, y1, x2, y2);
float AC = getDistance(x1, y1, x3, y3);
float BC = getDistance(x2, y2, x3, y3);
float s = (AB + BC + AC) / 2;
return (sqrt(s*(s-AB)*(s-BC)*(s-AC)));
}
/**
* @getDistFromPoint - Returns the distance from a point P to a line segment
* of two known vertices (x1, y1) and (x2, y2)
* @px1 - x coordinate of the abstract point P
* @px2 - y coordinate of the abstract point P
* @x3 - x coordinate of the first known vertex
* @y3 - y coordinate of the first known vertex
* @x4 - x coordinate of the second known vertex
* @y4 - y coordinate of the second known vertex
*/
float getDistFromPoint(float px1, float py1, float x2, float y2, float x3, float y3)
{
float area = getArea(px1, py1, x2, y2, x3, y3);
float AB = getDistance(x2, y2, x3, y3);
return (2 * area) / AB;
}