Line.cs 2.07 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
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
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Line
{
    public Point point1;
    public Point point2;
    public Vector3 direction;

    public State lineState = State.unknown;

    public Line(Point p1, Point p2)
    {
        point1 = p1;
        point2 = p2;
        direction = new Vector3(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z).normalized;
    }

    public float getLength()
    {
        return new Vector3(point2.x - point1.x, point2.y - point1.y, point2.z - point1.z).magnitude;
    }

    public void updateDirection()
    {
        direction = new Vector3(point2.x - point1.x, point2.y - point1.y, point2.z - point1.z).normalized;
        if (point1.getState() == State.confirm)
        {
            if (point2.getState() == State.confirm)
                lineState = State.confirm;
            else
                lineState = State.halfconfirm;
        }
        else if (point2.getState() == State.confirm)
            lineState = State.halfconfirm;
        else
            lineState = State.unknown;
    }

    public Vector3 getDirection(Point p)
    {
        Point tmp = getAnotherPoint(p);
        return new Vector3(p.x - tmp.x, p.y - tmp.y, p.z - tmp.z).normalized;
    }

    public Point getAnotherPoint(Point p)
    {
        if (p.name.Equals(point1.name))
            return point2;
        else if (p.name.Equals(point2.name))
            return point1;
        else
            return null;
    }

    public Gradient getGradient()
    {
        if(point2.getState() > point1.getState())
        {
            Vector3 tmp =  new Vector3(2 * point2.x - 2 * point1.x, 2 * point2.y - 2 * point1.y, 2 * point2.z - 2 * point1.z);
            return new Gradient(point2, tmp);
        }
        else
        {
            Vector3 tmp = new Vector3(2 * point1.x - 2 * point2.x, 2 * point1.y - 2 * point2.y, 2 * point1.z - 2 * point2.z);
            return new Gradient(point1, tmp);
        }
    }

    public void setConstraint(Constraint c)
    {
        point1.constraints.Add(c);
        point2.constraints.Add(c);
    }

}