Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec3cc85

Browse files
committed
add line/plane intersection schemes
1 parent 5c9409a commit ec3cc85

19 files changed

+769
-6140
lines changed

‎Assets/Coords.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Coords
6+
{
7+
8+
public float x;
9+
public float y;
10+
public float z;
11+
12+
public Coords(float _X, float _Y)
13+
{
14+
x = _X;
15+
y = _Y;
16+
z = -1;
17+
}
18+
19+
public Coords(float _X, float _Y, float _Z)
20+
{
21+
x = _X;
22+
y = _Y;
23+
z = _Z;
24+
}
25+
26+
public Coords(Vector3 vecpos)
27+
{
28+
x = vecpos.x;
29+
y = vecpos.y;
30+
z = vecpos.z;
31+
}
32+
33+
public override string ToString()
34+
{
35+
return "(" + x + "," + y + "," + z + ")";
36+
}
37+
38+
public Vector3 ToVector()
39+
{
40+
return new Vector3(x, y, z);
41+
}
42+
43+
static public Coords operator +(Coords a, Coords b)
44+
{
45+
return new Coords(a.x + b.x, a.y + b.y, a.z + b.z);
46+
}
47+
48+
static public Coords operator -(Coords a, Coords b)
49+
{
50+
return new Coords(a.x - b.x, a.y - b.y, a.z - b.z);
51+
}
52+
53+
static public void DrawLine(Coords startPoint, Coords endPoint, float width, Color colour)
54+
{
55+
GameObject line = new GameObject("Line_" + startPoint.ToString() + "_" + endPoint.ToString());
56+
LineRenderer lineRenderer = line.AddComponent<LineRenderer>();
57+
lineRenderer.material = new Material(Shader.Find("Unlit/Color"));
58+
lineRenderer.material.color = colour;
59+
lineRenderer.positionCount = 2;
60+
lineRenderer.SetPosition(0, new Vector3(startPoint.x, startPoint.y, startPoint.z));
61+
lineRenderer.SetPosition(1, new Vector3(endPoint.x, endPoint.y, endPoint.z));
62+
lineRenderer.startWidth = width;
63+
lineRenderer.endWidth = width;
64+
}
65+
66+
67+
static public Coords Perp(Coords v)
68+
{
69+
return new Coords(-v.y, v.x);
70+
}
71+
72+
static public void DrawPoint(Coords position, float width, Color colour)
73+
{
74+
GameObject line = new GameObject("Point_" + position.ToString());
75+
LineRenderer lineRenderer = line.AddComponent<LineRenderer>();
76+
lineRenderer.material = new Material(Shader.Find("Unlit/Color"));
77+
lineRenderer.material.color = colour;
78+
lineRenderer.positionCount = 2;
79+
lineRenderer.SetPosition(0, new Vector3(position.x - width / 3.0f, position.y - width / 3.0f, position.z));
80+
lineRenderer.SetPosition(1, new Vector3(position.x + width / 3.0f, position.y + width / 3.0f, position.z));
81+
lineRenderer.startWidth = width;
82+
lineRenderer.endWidth = width;
83+
}
84+
85+
}

‎Assets/Scenes/CartesianPlayground/Coords.cs.meta renamed to ‎Assets/Coords.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/CreateLines.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class CreateLines : MonoBehaviour
6+
{
7+
Line L1;
8+
Line L2;
9+
10+
// Start is called before the first frame update
11+
void Start()
12+
{
13+
L1 = new Line(new Coords(0.0f, 0.0f, 0.0f), new Coords(-6.0f, 7.0f, 3.0f));
14+
L1.Draw(1, Color.green);
15+
L2 = new Line(new Coords(0.0f, 0.0f, 0.0f), new Coords(-7.0f, -6.0f, 6.0f));
16+
L2.Draw(1, Color.red);
17+
18+
float intersectT = L1.IntersectsAt(L2);
19+
float intersectS = L2.IntersectsAt(L1);
20+
21+
if (intersectT == intersectT && intersectS == intersectS)
22+
{
23+
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
24+
sphere.transform.position = L1.Lerp(intersectT).ToVector();
25+
}
26+
}
27+
28+
// Update is called once per frame
29+
void Update()
30+
{
31+
32+
}
33+
}

‎Assets/Scenes/CartesianPlayground/DrawGraph.cs.meta renamed to ‎Assets/CreateLines.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/HolisticMath.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class HolisticMath
6+
{
7+
static public float Square(float value)
8+
{
9+
return value * value;
10+
}
11+
12+
static public float Distance(Coords point1, Coords point2)
13+
{
14+
float diffSquared = Square(point1.x - point2.x) +
15+
Square(point1.y - point2.y) +
16+
Square(point1.z - point2.z);
17+
float squareRoot = Mathf.Sqrt(diffSquared);
18+
//if you are interested in how the Sqrt is calculated see
19+
//https://www.codeproject.com/Articles/570700/SquareplusRootplusalgorithmplusforplusC
20+
return squareRoot;
21+
22+
}
23+
24+
static public Coords Lerp(Coords A, Coords B, float t)
25+
{
26+
t = Mathf.Clamp(t, 0, 1);
27+
28+
Coords v = new Coords(B.x - A.x, B.y - A.y, B.z - A.z);
29+
30+
float xt = A.x + v.x * t;
31+
float yt = A.y + v.y * t;
32+
float zt = A.z + v.z * t;
33+
34+
return new Coords(xt, yt, zt);
35+
}
36+
37+
static public Coords GetNormal(Coords vector)
38+
{
39+
float length = Distance(new Coords(0, 0, 0), vector);
40+
vector.x /= length;
41+
vector.y /= length;
42+
vector.z /= length;
43+
44+
return vector;
45+
}
46+
47+
static public float Dot(Coords vector1, Coords vector2)
48+
{
49+
return (vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z);
50+
}
51+
52+
static public float Angle(Coords vector1, Coords vector2)
53+
{
54+
float dotDivide = Dot(vector1, vector2) / (Distance(new Coords(0, 0, 0), vector1) * Distance(new Coords(0, 0, 0), vector2));
55+
return Mathf.Acos(dotDivide); //radians. For degrees * 180/Mathf.PI;
56+
}
57+
58+
static public Coords Cross(Coords vector1, Coords vector2)
59+
{
60+
float iMult = vector1.y * vector2.z - vector1.z * vector2.y;
61+
float jMult = vector1.z * vector2.x - vector1.x * vector2.z;
62+
float kMult = vector1.x * vector2.y - vector1.y * vector2.x;
63+
Coords crossProd = new Coords(iMult, jMult, kMult);
64+
return crossProd;
65+
}
66+
67+
static public Coords Rotate(Coords vector, float angle, bool clockwise)
68+
//angle in radians please
69+
{
70+
if (clockwise)
71+
angle = 2*Mathf.PI - angle;
72+
73+
float xVal = vector.x * Mathf.Cos(angle) - vector.y * Mathf.Sin(angle);
74+
float yVal = vector.x * Mathf.Sin(angle) + vector.y * Mathf.Cos(angle);
75+
return new Coords(xVal, yVal, 0);
76+
}
77+
}

‎Assets/Move.cs.meta renamed to ‎Assets/HolisticMath.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Line.cs

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,71 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
3-
using Unity.VisualScripting;
43
using UnityEngine;
54

65
public class Line
76
{
8-
public Vector3 A;
9-
public Vector3 B;
10-
public Vector3 v;
7+
Coords A, B, v;
8+
9+
public enum LINETYPE
10+
{
11+
LINE,
12+
SEGMENT,
13+
RAY
14+
}
1115

12-
public enum LINETYPE { LINE, SEGMENT, RAY };
1316
LINETYPE type;
1417

15-
public Line(Vector3A,Vector3B, LINETYPE type)
18+
public Line(Coords_A,Coords_B, LINETYPE _type)
1619
{
17-
this.A = A;
18-
this.B = B;
19-
this.v = B-A;
20-
this.type = type;
20+
A = _A;
21+
B = _B;
22+
type = _type;
23+
v = newCoords(B.x-A.x,B.y-A.y,B.z-A.z);
2124
}
2225

23-
public Vector3Lerp(floatt)
26+
public Line(Coords_A,Coords_V)
2427
{
25-
if (type == LINETYPE.LINE)
26-
{
27-
t = Mathf.Clamp(t, float.MinValue, float.MaxValue);
28-
}
29-
else if (type == LINETYPE.SEGMENT)
28+
A = _A;
29+
v = _V;
30+
B = _A + v;
31+
type = LINETYPE.SEGMENT;
32+
}
33+
34+
35+
public float IntersectsAt(Line l)
36+
{
37+
if(HolisticMath.Dot(Coords.Perp(l.v), v) == 0)
3038
{
31-
t=Mathf.Clamp01(t);
39+
returnfloat.NaN;
3240
}
33-
else if (type == LINETYPE.RAY)
41+
42+
Coords c = l.A - this.A;
43+
float t = HolisticMath.Dot(Coords.Perp(l.v), c) / HolisticMath.Dot(Coords.Perp(l.v), v);
44+
45+
if(t < 0 || t > 1 && type == LINETYPE.SEGMENT)
3446
{
35-
t=Mathf.Clamp(t,0,float.MaxValue);
47+
returnfloat.NaN;
3648
}
3749

38-
return A + (v * t);
50+
return t;
51+
}
52+
53+
public void Draw(float width, Color col)
54+
{
55+
Coords.DrawLine(A, B, width, col);
56+
}
57+
58+
public Coords Lerp(float t)
59+
{
60+
if (type == LINETYPE.SEGMENT)
61+
t = Mathf.Clamp(t, 0, 1);
62+
else if (type == LINETYPE.RAY && t < 0)
63+
t = 0;
64+
65+
float xt = A.x + v.x * t;
66+
float yt = A.y + v.y * t;
67+
float zt = A.z + v.z * t;
68+
69+
return new Coords(xt, yt, zt);
3970
}
4071
}

‎Assets/Move.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎Assets/Plane.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Plane
6+
{
7+
Coords A;
8+
Coords B;
9+
Coords C;
10+
Coords v;
11+
Coords u;
12+
13+
public Plane(Coords _A, Coords _B, Coords _C)
14+
{
15+
A = _A;
16+
B = _B;
17+
C = _C;
18+
v = B - A;
19+
u = C - A;
20+
}
21+
22+
public Plane(Coords _A, Vector3 V, Vector3 U)
23+
{
24+
A = _A;
25+
v = new Coords(V.x, V.y, V.z);
26+
u = new Coords(U.x, U.y, U.z);
27+
}
28+
29+
public Coords Lerp(float s, float t)
30+
{
31+
float xst = A.x + v.x * s + u.x * t;
32+
float yst = A.y + v.y * s + u.y * t;
33+
float zst = A.z + v.z * s + u.z * t;
34+
35+
return new Coords(xst, yst, zst);
36+
}
37+
}

‎Assets/Scenes/CartesianPlayground/DrawLines.cs.meta renamed to ‎Assets/Plane.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /