Skip to content

Commit f31b16f

Browse files
author
Mogoson
committed
Upload demo files
1 parent a90d8e9 commit f31b16f

22 files changed

+490
-7
lines changed

README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
# MGS.Mathematics
1+
[TOC]
2+
3+
# MGS.Mathematics
4+
5+
## Summary
6+
7+
- Mathematics code for C# and Unity project develop.
8+
9+
## Environment
10+
11+
- Unity 5.0 or above.
12+
- .Net Framework 3.5 or above.
13+
14+
## Platform
15+
16+
- Windows
17+
18+
## Module
19+
### Geometry
20+
21+
- Vector2D, Line, Circle.
22+
- Relation, Intersection.
23+
24+
### Hermite Curve
25+
26+
- Hermite polynomial.
27+
- Hermite Curve.
28+
29+
### Math Smooth
30+
31+
- Linear Smooth.
32+
- Quadratic Smooth.
33+
- Cubic Smooth.
34+
35+
## Demo
36+
37+
- Demos in the path "MGS.Packages/Mathematics/Demo/" provide reference to you.
38+
39+
------
40+
41+
Copyright © 2022 Mogoson. mogoson@outlook.com
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*************************************************************************
2+
* Copyright © 2021 Mogoson. All rights reserved.
3+
*------------------------------------------------------------------------
4+
* File : HelpHUD.cs
5+
* Description : Draw help HUD in scene.
6+
*------------------------------------------------------------------------
7+
* Author : Mogoson
8+
* Version : 0.1.0
9+
* Date : 4/15/2018
10+
* Description : Initial development version.
11+
*************************************************************************/
12+
13+
using UnityEngine;
14+
15+
namespace MGS.Packages.Demo
16+
{
17+
[ExecuteInEditMode]
18+
public class HelpHUD : MonoBehaviour
19+
{
20+
#region Field and Property
21+
[Multiline]
22+
public string info = "Help info.";
23+
public Color color = Color.black;
24+
25+
public float top = 10;
26+
public float left = 10;
27+
#endregion
28+
29+
#region Private Method
30+
private void OnGUI()
31+
{
32+
DrawHelpHUD();
33+
}
34+
35+
private void DrawHelpHUD()
36+
{
37+
GUI.color = color;
38+
GUILayout.Space(top);
39+
GUILayout.BeginHorizontal();
40+
GUILayout.Space(left);
41+
GUILayout.Label(info);
42+
GUILayout.EndHorizontal();
43+
}
44+
#endregion
45+
}
46+
}

UnityProject/Assets/MGS.Packages/HelpHUD.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Materials.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Materials/Blue.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Materials/Red.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Materials/Yellow.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Scripts.meta

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*************************************************************************
2+
* Copyright © 2022 Mogoson. All rights reserved.
3+
*------------------------------------------------------------------------
4+
* File : HermiteCurveDemo.cs
5+
* Description : Null.
6+
*------------------------------------------------------------------------
7+
* Author : Mogoson
8+
* Version : 1.0.0
9+
* Date : 11/29/2022
10+
* Description : Initial development version.
11+
*************************************************************************/
12+
13+
using System.Collections.Generic;
14+
using UnityEngine;
15+
16+
namespace MGS.Mathematics.Demo
17+
{
18+
public class HermiteCurveDemo : MonoBehaviour
19+
{
20+
LineRenderer lineRenderer;
21+
22+
void Start()
23+
{
24+
lineRenderer = GetComponent<LineRenderer>();
25+
}
26+
27+
void Update()
28+
{
29+
if (Input.GetKeyDown(KeyCode.H))
30+
{
31+
var fs = new List<KeyFrame>();
32+
foreach (Transform item in transform)
33+
{
34+
fs.Add(new KeyFrame(item.position.x, item.position.y));
35+
}
36+
37+
var curve = new HermiteCurve(fs.ToArray());
38+
curve.SmoothTangents();
39+
40+
var start = fs[0].time;
41+
var end = fs[fs.Count - 1].time;
42+
var delta = (end - start) / 30.0f;
43+
var timer = start;
44+
var index = 0;
45+
while (timer < end)
46+
{
47+
var fm = curve.Evaluate(timer);
48+
var pos = new Vector3(timer, fm);
49+
lineRenderer.SetPosition(index, pos);
50+
timer += delta;
51+
index++;
52+
}
53+
}
54+
}
55+
}
56+
}

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Scripts/HermiteCurveDemo.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*************************************************************************
2+
* Copyright © 2022 Mogoson. All rights reserved.
3+
*------------------------------------------------------------------------
4+
* File : IntersectionDemo.cs
5+
* Description : Null.
6+
*------------------------------------------------------------------------
7+
* Author : Mogoson
8+
* Version : 1.0.0
9+
* Date : 11/29/2022
10+
* Description : Initial development version.
11+
*************************************************************************/
12+
13+
using UnityEngine;
14+
15+
namespace MGS.Mathematics.Demo
16+
{
17+
public class IntersectionDemo : MonoBehaviour
18+
{
19+
public Transform circle0;
20+
public Transform circle1;
21+
public Transform line;
22+
public Transform[] Intersections;
23+
24+
void Update()
25+
{
26+
if (Input.GetKeyDown(KeyCode.I))
27+
{
28+
foreach (var item in Intersections)
29+
{
30+
item.gameObject.SetActive(false);
31+
}
32+
33+
var c0 = new Circle(new Vector2D(circle0.position.x, circle0.position.y), circle0.localScale.z * 0.5f);
34+
var c1 = new Circle(new Vector2D(circle1.position.x, circle1.position.y), circle1.localScale.z * 0.5f);
35+
var psCC = Geometry.GetIntersections(c0, c1);
36+
if (psCC != null && psCC.Count > 0)
37+
{
38+
Intersections[0].gameObject.SetActive(true);
39+
Intersections[0].position = new Vector3((float)psCC[0].x, (float)psCC[0].y);
40+
if (psCC.Count > 1)
41+
{
42+
Intersections[1].gameObject.SetActive(true);
43+
Intersections[1].position = new Vector3((float)psCC[1].x, (float)psCC[1].y);
44+
}
45+
}
46+
47+
var offset = line.position + line.right;
48+
var p0 = new Vector2D(line.position.x, line.position.y);
49+
var p1 = new Vector2D(offset.x, offset.y);
50+
var L = Line.FromPoints(p0, p1);
51+
var psCL = Geometry.GetIntersections(c1, L);
52+
if (psCL != null && psCL.Count > 0)
53+
{
54+
Intersections[2].gameObject.SetActive(true);
55+
Intersections[2].position = new Vector3((float)psCL[0].x, (float)psCL[0].y);
56+
if (psCL.Count > 1)
57+
{
58+
Intersections[3].gameObject.SetActive(true);
59+
Intersections[3].position = new Vector3((float)psCL[1].x, (float)psCL[1].y);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Scripts/IntersectionDemo.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*************************************************************************
2+
* Copyright © 2022 Mogoson. All rights reserved.
3+
*------------------------------------------------------------------------
4+
* File : MathSmoothDemo.cs
5+
* Description : Null.
6+
*------------------------------------------------------------------------
7+
* Author : Mogoson
8+
* Version : 1.0.0
9+
* Date : 11/29/2022
10+
* Description : Initial development version.
11+
*************************************************************************/
12+
13+
using System.Collections.Generic;
14+
using UnityEngine;
15+
16+
namespace MGS.Mathematics.Demo
17+
{
18+
public class MathSmoothDemo : MonoBehaviour
19+
{
20+
public LineRenderer[] lines;
21+
22+
void Update()
23+
{
24+
if (Input.GetKeyDown(KeyCode.S))
25+
{
26+
var ps = new List<Vector3>();
27+
var ys = new List<float>();
28+
foreach (Transform item in transform)
29+
{
30+
ps.Add(item.position);
31+
ys.Add(item.position.y);
32+
}
33+
34+
var lps = LinearSmooth.SevenPointSmooth(ys.ToArray());
35+
for (int i = 0; i < lps.Length; i++)
36+
{
37+
var pos = new Vector3(ps[i].x, lps[i], ps[i].z);
38+
lines[0].SetPosition(i, pos);
39+
}
40+
41+
var qps = QuadraticSmooth.SevenPointSmooth(ys.ToArray());
42+
for (int i = 0; i < qps.Length; i++)
43+
{
44+
var pos = new Vector3(ps[i].x, qps[i], ps[i].z);
45+
lines[1].SetPosition(i, pos);
46+
}
47+
48+
var cps = CubicSmooth.SevenPointSmooth(ys.ToArray());
49+
for (int i = 0; i < cps.Length; i++)
50+
{
51+
var pos = new Vector3(ps[i].x, cps[i], ps[i].z);
52+
lines[2].SetPosition(i, pos);
53+
}
54+
}
55+
}
56+
}
57+
}

UnityProject/Assets/MGS.Packages/Mathematics/Demo/Scripts/MathSmoothDemo.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)