少年修仙传客户端代码仓库
hch
2025-06-12 204ef05a831c9484e2abc561d27ecbff7c797453
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
79
80
81
82
83
84
using UnityEngine;
 
public struct Line2D
{
    public Vector2 start;
    public Vector2 end;
 
    public float slope
    {
        get
        {
            if (end.x == start.x)
                return 0;
            return (end.y - start.y) / (end.x - start.x);
        }
    }
 
    public float perpSlope
    {
        get
        {
            return -1 / slope;
        }
    }
 
    /// <summary>
    /// 计算给定点是否在线的左边
    /// </summary>
    /// <param name="point">给定点</param>
    /// <param name="line">线</param>
    /// <returns>是否在左边</returns>
    public static bool PointAtLineLeft(Vector2 point, Line2D line)
    {
        return PointRelation(line, point) > 0;
    }
 
    /// <summary>
    /// 计算给定点是否在线的右边
    /// </summary>
    /// <param name="point">给定点</param>
    /// <param name="line">线</param>
    /// <returns>是否在右边</returns>
    public static bool PointAtLineRight(Vector2 point, Line2D line)
    {
        return PointRelation(line, point) < 0;
    }
 
    /// <summary>
    /// 计算两个斜率是否垂直
    /// </summary>
    /// <param name="slope1">斜率1</param>
    /// <param name="slope2">斜率2</param>
    /// <returns>是否垂直</returns>
    public static bool IsPerp(float slope1, float slope2)
    {
        return (slope1 * slope2 == -1);
    }
 
    /// <summary>
    /// 计算两条线的交点
    /// </summary>
    /// <param name="line1">给定的线1</param>
    /// <param name="line2">给定的线2</param>
    /// <returns>交点</returns>
    public static Vector2 Line2DIntersect(Line2D line1, Line2D line2)
    {
        Vector2 _intersectPoint;
 
        _intersectPoint.x = (line1.slope * line1.start.x - line2.slope * line2.start.x + line2.start.y - line1.start.y) / (line1.slope - line2.slope);
        _intersectPoint.y = line1.slope * (_intersectPoint.x - line1.start.x) + line1.start.y;
 
        return _intersectPoint;
    }
 
    private static float PointRelation(Line2D line, Vector2 pt)
    {
        return (pt.y - line.start.y) * (line.end.x - line.start.x) - (pt.x - line.start.x) * (line.end.y - line.start.y);
    }
 
    public void DebugDraw(Color color)
    {
        Debug.DrawLine(new Vector3(start.x, 0, start.y), new Vector3(end.x, 0, end.y), color);
    }
}