| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using o0.Geometry2D.Float;
- using System;
- using UnityEngine;
- namespace ZIM.Unity
- {
- public static partial class ZIMMath
- {
- // 向量与x轴的夹角,返回0-360度
- public static float DegreeToXAxis(this Vector v)
- {
- //var a = v.x > 0 ? Math.Atan(v.y / v.x) * 180 / Math.PI : 180 + Math.Atan(v.y / v.x) * 180 / Math.PI;
- var a = Math.Atan2(v.y, v.x) * 180 / Math.PI;
- if (a < 0)
- a += 360;
- return (float)a;
- }
- // Projects a vector onto another vector.
- public static Vector2 Project(this Vector2 vector, Vector2 onNormal)
- {
- float dotProduct = Vector2.Dot(vector, onNormal);
- float magnitudeSquared = onNormal.sqrMagnitude;
- if (magnitudeSquared < Mathf.Epsilon)
- return Vector2.zero;
- return (dotProduct / magnitudeSquared) * onNormal;
- }
- public static float LengthManhattan(this Vector2 v) => Math.Abs(v.x) + Math.Abs(v.y);
- public static double LengthManhattan(Vector2 v1, Vector2 v2) => (v1 - v2).LengthManhattan();
- // 可以用于判断点位V于直线AB哪一侧
- // return的值 等于0: 点在线上,大于0: 三角形ABV是逆时针,小于0: 三角形ABV是顺时针 (注:Line的构造函数会修改AB的顺序)
- public static float LineCrossWithPoint(this Line l, Vector v)
- {
- var ld = l.B - l.A;
- var vd = v - l.A;
- return ld.x * vd.y - ld.y * vd.x;
- }
- }
- }
|