Plane.cs 1.48 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
1
2
3
4
5
6
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Plane
{
BlackAngle233's avatar
5.31    
BlackAngle233 committed
7
8
    public string name;

BlackAngle233's avatar
BlackAngle233 committed
9
10
11
12
13
    List<Point> points;
    public Vector3 normal;

    public Plane(List<Point> ps)
    {
BlackAngle233's avatar
5.31    
BlackAngle233 committed
14
        name = "";
BlackAngle233's avatar
BlackAngle233 committed
15
16
17
18
        points = new List<Point>();
        foreach(Point p in ps)
        {
            points.Add(p);
BlackAngle233's avatar
5.31    
BlackAngle233 committed
19
            name += p.name;
BlackAngle233's avatar
BlackAngle233 committed
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
        }

        updateNormal();
    }

    public void updateNormal()
    {
        List<Vector3> vector3s = new List<Vector3>();
        for (int i = 1; i < points.Count; ++i)
        {
            Vector3 tmp = new Vector3(points[i].x - points[0].x, points[i].y - points[0].y, points[i].z - points[0].z);
            vector3s.Add(tmp);
        }

        List<Vector3> normals = new List<Vector3>();
        for (int i = 0; i < vector3s.Count; ++i)
        {
            for (int j = i + 1; j < vector3s.Count; ++j)
            {
                Vector3 tmp = Vector3.Cross(vector3s[i], vector3s[j]).normalized;
                if (tmp.magnitude != 0)
                    normals.Add(tmp);
            }
        }

        bool flag = true;
        for (int i = 1; i < normals.Count; ++i)
        {
            if (normals[i - 1] != normals[i] && normals[i - 1] != -normals[i])
            {
                flag = false;
                break;
            }
        }
BlackAngle233's avatar
5.31    
BlackAngle233 committed
54
        if (flag && normals.Count != 0)
BlackAngle233's avatar
BlackAngle233 committed
55
56
57
58
59
60
61
62
63
        {
            normal = normals[0];
        }
        else
        {
            Debug.Log("wrong plane");
        }
    }
}