miket 11 月之前
父节点
当前提交
7c7df5e4d8
共有 3 个文件被更改,包括 48 次插入0 次删除
  1. 4 0
      geometry/geometry.h
  2. 5 0
      geometry/point.h
  3. 39 0
      geometry/polygon.cpp

+ 4 - 0
geometry/geometry.h

@@ -1,3 +1,5 @@
+#ifndef _GEOMETRY_F_H
+#define _GEOMETRY_F_H
 
 #include <vector>
 #include "point.h"
@@ -18,3 +20,5 @@ public:
 
 };
 
+#endif 
+

+ 5 - 0
geometry/point.h

@@ -1,3 +1,6 @@
+#ifndef _GEOMETRY_H
+#define _GEOMETRY_H
+
 #include<iostream>;
 #include <cmath>;
 
@@ -26,3 +29,5 @@ inline Point::Point(value_class ax, value_class ay)
 	x = (double)(ax);
 	y = (double)(ay);
 }
+#endif // !_GEOMETRY_H
+

+ 39 - 0
geometry/polygon.cpp

@@ -0,0 +1,39 @@
+#include "polygon.h"
+
+void Polygon::calc_sides()
+{
+	for (int i = 0; i < points.size() - 1; i++) {
+		sides.push_back(points[i].calc_distance(points[i + 1]));
+	}
+	sides.push_back(points[points.size() - 1].calc_distance(points[0]));
+}
+
+Polygon::Polygon()
+{
+}
+
+double Polygon::calc_area()
+{
+	double res=0.0;
+	for (int i = 0; i < points.size() - 1; i++) {
+		res += points[i].x * points[i + 1].y;
+		res += points[i + 1].x * points[i].y;
+	}
+	res += points[points.size()-1].x * points[0].y;
+	res += points[points.size()-1].y * points[0].x;
+
+	return res * 0.5;
+}
+
+double Polygon::calc_perimeter()
+{
+	double res = 0.0;
+	for (int i = 0; i < sides.size(); i++)
+		res += sides[i];
+	return res;
+}
+
+void Polygon::name()
+{
+	cout << "Polygon \n";
+}