6 using System.Collections.Generic;
17 public Stack<Polygon> Polygons =
new Stack<Polygon>();
26 public void AddPoint(GameObject LinePrefab, GameObject PointPrefab, GameObject TextPrefab)
29 var point = (GameObject)Instantiate(PointPrefab, hitPoint, Quaternion.identity);
30 var newPoint =
new Point 40 Root =
new GameObject(),
41 Points =
new List<Vector3>()
44 CurrentPolygon.
Points.Add(newPoint.Position);
45 newPoint.Root.transform.parent = CurrentPolygon.
Root.transform;
49 CurrentPolygon.
Points.Add(newPoint.Position);
50 newPoint.Root.transform.parent = CurrentPolygon.
Root.transform;
51 if (CurrentPolygon.
Points.Count > 1)
53 var index = CurrentPolygon.
Points.Count - 1;
54 var centerPos = (CurrentPolygon.
Points[index] + CurrentPolygon.
Points[index - 1]) * 0.5f;
55 var direction = CurrentPolygon.
Points[index] - CurrentPolygon.
Points[index - 1];
56 var distance = Vector3.Distance(CurrentPolygon.
Points[index], CurrentPolygon.
Points[index - 1]);
57 var line = (GameObject)Instantiate(LinePrefab, centerPos, Quaternion.LookRotation(direction));
58 line.transform.localScale =
new Vector3(distance, 0.005f, 0.005f);
59 line.transform.Rotate(Vector3.down, 90f);
60 line.transform.parent = CurrentPolygon.
Root.transform;
72 public void ClosePolygon(GameObject LinePrefab, GameObject TextPrefab)
74 if (CurrentPolygon != null)
77 var area = CalculatePolygonArea(CurrentPolygon);
78 var index = CurrentPolygon.
Points.Count - 1;
79 var centerPos = (CurrentPolygon.
Points[index] + CurrentPolygon.
Points[0]) * 0.5f;
80 var direction = CurrentPolygon.
Points[index] - CurrentPolygon.
Points[0];
81 var distance = Vector3.Distance(CurrentPolygon.
Points[index], CurrentPolygon.
Points[0]);
82 var line = (GameObject)Instantiate(LinePrefab, centerPos, Quaternion.LookRotation(direction));
83 line.transform.localScale =
new Vector3(distance, 0.005f, 0.005f);
84 line.transform.Rotate(Vector3.down, 90f);
85 line.transform.parent = CurrentPolygon.
Root.transform;
87 var vect =
new Vector3(0, 0, 0);
88 foreach (var point
in CurrentPolygon.
Points)
92 var centerPoint = vect / (index + 1);
93 var direction1 = CurrentPolygon.
Points[1] - CurrentPolygon.
Points[0];
94 var directionF = Vector3.Cross(direction, direction1);
95 var tip = (GameObject)Instantiate(TextPrefab, centerPoint, Quaternion.LookRotation(directionF));
98 tip.GetComponent<TextMesh>().text = area +
"㎡";
99 tip.transform.parent = CurrentPolygon.
Root.transform;
100 Polygons.Push(CurrentPolygon);
109 if (Polygons != null && Polygons.Count > 0)
111 while (Polygons.Count > 0)
113 var lastLine = Polygons.Pop();
114 Destroy(lastLine.Root);
122 if (Polygons != null && Polygons.Count > 0)
124 var lastLine = Polygons.Pop();
125 Destroy(lastLine.Root);
136 private float CalculateTriangleArea(Vector3 p1, Vector3 p2, Vector3 p3)
138 var a = Vector3.Distance(p1, p2);
139 var b = Vector3.Distance(p1, p3);
140 var c = Vector3.Distance(p3, p2);
141 var p = (a + b + c) / 2f;
142 var s = Mathf.Sqrt(p * (p - a) * (p - b) * (p - c));
151 private float CalculatePolygonArea(
Polygon polygon)
155 var n = polygon.
Points.Count;
156 for (; i < n - 1; i++)
157 s += CalculateTriangleArea(polygon.
Points[0], polygon.
Points[i], polygon.
Points[i + 1]);
158 return 0.5f * Mathf.Abs(s);
167 Root =
new GameObject(),
168 Points =
new List<Vector3>()
178 if (CurrentPolygon != null && !CurrentPolygon.
IsFinished)
180 Destroy(CurrentPolygon.
Root);
184 Root =
new GameObject(),
185 Points =
new List<Vector3>()
194 public float Area {
get;
set; }
196 public List<Vector3> Points {
get;
set; }
198 public GameObject Root {
get;
set; }
200 public bool IsFinished {
get;
set; }