Skip to content

Commit 7a9c162

Browse files
committed
feat: Add rotate point around
1 parent 3b43ea9 commit 7a9c162

File tree

2 files changed

+155
-143
lines changed

2 files changed

+155
-143
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Mathf.cs

Lines changed: 148 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ public static int OverFlowIndex(int targetIndex, int length)
237237
/// </summary>
238238
/// <param name="currentVal"></param>
239239
/// <param name="targetVal"></param>
240-
/// <returns></returns>
241240
public static int ApproachTo(int currentVal, int targetVal)
242241
{
243242
if (currentVal == targetVal)
@@ -253,69 +252,6 @@ public static int ApproachTo(int currentVal, int targetVal)
253252
return 0;
254253
}
255254

256-
/// <summary>
257-
/// 計算旋轉的點 (JCS_VECTOR2F)
258-
/// </summary>
259-
/// <param name="point"> 我們要計算的"點" </param>
260-
/// <param name="cos"> Cos 的角度 "Cos(angle)" </param>
261-
/// <param name="sin"> Sin 的角度 "Sin(angle)" </param>
262-
/// <param name="origin"> 以這個"點"為中心旋轉 </param>
263-
/// <returns></returns>
264-
public static Vector3 RotatePointX(
265-
Vector3 point, float cos, float sin, Vector3 origin)
266-
{
267-
return new Vector3(
268-
point.x,
269-
origin.z + ((point.z - origin.z) * cos) - ((point.y - origin.y) * sin),
270-
origin.y + ((point.z - origin.z) * sin) + ((point.y - origin.y) * cos));
271-
}
272-
public static Vector3 RotatePointZ(
273-
Vector3 point, float cos, float sin, Vector3 origin)
274-
{
275-
return new Vector3(
276-
origin.x + ((point.x - origin.x) * cos) - ((point.y - origin.y) * sin),
277-
origin.y + ((point.x - origin.x) * sin) + ((point.y - origin.y) * cos),
278-
point.z);
279-
}
280-
public static Vector3 RotatePointY(
281-
Vector3 point, float cos, float sin, Vector3 origin)
282-
{
283-
return new Vector3(
284-
origin.x + ((point.x - origin.x) * cos) - ((point.z - origin.z) * sin),
285-
point.y,
286-
origin.z + ((point.x - origin.x) * sin) + ((point.z - origin.z) * cos));
287-
}
288-
289-
/// <summary>
290-
/// 計算旋轉的點 (JCS_VECTOR2F)
291-
/// </summary>
292-
/// <param name="point"> 我們要計算的"點" </param>
293-
/// <param name="cos"> Cos 的角度 "Cos(angle)" </param>
294-
/// <param name="sin"> Sin 的角度 "Sin(angle)" </param>
295-
/// <param name="origin"> 以這個"點"為中心旋轉 </param>
296-
/// <returns></returns>
297-
public static Vector3 RotatePointX(Vector3 point, Vector3 origin, float angle)
298-
{
299-
return new Vector3(
300-
point.x,
301-
origin.y + ((Mathf.Cos(angle) * (point.y - origin.y))) - (Mathf.Sin(angle) * (point.z - origin.z)),
302-
origin.z + ((Mathf.Sin(angle) * (point.y - origin.y))) + (Mathf.Cos(angle) * (point.z - origin.z)));
303-
}
304-
public static Vector3 RotatePointY(Vector3 point, Vector3 origin, float angle)
305-
{
306-
return new Vector3(
307-
origin.x + ((Mathf.Cos(angle) * (point.x - origin.x))) - (Mathf.Sin(angle) * (point.z - origin.z)),
308-
point.y,
309-
origin.z + ((Mathf.Sin(angle) * (point.x - origin.x))) + (Mathf.Cos(angle) * (point.z - origin.z)));
310-
}
311-
public static Vector3 RotatePointZ(Vector3 point, Vector3 origin, float angle)
312-
{
313-
return new Vector3(
314-
origin.x + ((Mathf.Cos(angle) * (point.x - origin.x))) - (Mathf.Sin(angle) * (point.y - origin.y)),
315-
origin.y + ((Mathf.Sin(angle) * (point.x - origin.x))) + (Mathf.Cos(angle) * (point.y - origin.y)),
316-
point.z);
317-
}
318-
319255
/// <summary>
320256
/// check if the vector value is infinity
321257
/// </summary>
@@ -486,7 +422,7 @@ public static float PythagoreanTheorem(float s1, float s2, TriSides type)
486422
}
487423
}
488424

489-
JCS_Debug.LogError("This not suppose to happen here...");
425+
JCS_Debug.LogError("This not suppose to happen here");
490426

491427
return 0;
492428
}
@@ -515,7 +451,6 @@ public static int GetSingleDigit(int digit, int number)
515451
return remainder / divider;
516452
}
517453

518-
519454
/// <summary>
520455
/// Count the digit by pass in number you want to count.
521456
///
@@ -568,7 +503,6 @@ public static float CrossMultiply(float x, float y, float a)
568503
return (a * y / x);
569504
}
570505

571-
572506
/// <summary>
573507
/// Convert degree to radian.
574508
/// </summary>
@@ -619,6 +553,153 @@ public static float Tan(float deg)
619553
return Mathf.Tan(DegreeToRadian(deg));
620554
}
621555

556+
/// <summary>
557+
/// Truncate float number.
558+
///
559+
/// If 'digits'=2 and 'value'=1.345698F:
560+
/// 1.345698F => 1.34
561+
///
562+
/// If 'digits'=2 and 'value'=1.300000F:
563+
/// 1.300000F => 1.30
564+
///
565+
/// SOURCE(jenchieh): https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8092fd2-1080-416c-8ae1-2bad8c013a21/how-to-round-off-a-float-to-2-decimal-places?forum=csharpgeneral
566+
/// </summary>
567+
/// <param name="val"> Value to do truncate. </param>
568+
/// <param name="digits"> Target shown digit. </param>
569+
/// <returns> Result value. </returns>
570+
public static float Truncate(float val, int digits)
571+
{
572+
double mult = Math.Pow(10.0, digits);
573+
double result = Math.Truncate(mult * val) / mult;
574+
return (float)result;
575+
}
576+
577+
/// <summary>
578+
/// Find the greatest common factor.
579+
/// 最大公因數.
580+
///
581+
/// SOURCE: https://stackoverflow.com/questions/18541832/c-sharp-find-the-greatest-common-divisor
582+
/// AUTHOR: Drew Noakes
583+
/// </summary>
584+
/// <param name="a"> number a. </param>
585+
/// <param name="b"> number b. </param>
586+
/// <returns>
587+
/// greatest common factor for 'a' nd 'b'.
588+
/// </returns>
589+
public static int GCD(int a, int b)
590+
{
591+
while (a != 0 && b != 0)
592+
{
593+
if (a > b)
594+
a %= b;
595+
else
596+
b %= a;
597+
}
598+
599+
return (a == 0) ? b : a;
600+
}
601+
602+
/// <summary>
603+
/// Return the positive/negative 1 sign from VAL.
604+
///
605+
/// If the value is 0 then return 0.
606+
/// </summary>
607+
/// <param name="val"> Value you want to idenfity. </param>
608+
/// <returns>
609+
/// Return 0, if the VAL can't be identify.
610+
/// Return 1, if the VAL is positive value.
611+
/// Return -1, if the VAL is negative value.
612+
/// </returns>
613+
public static float GetSign(float val)
614+
{
615+
if (val == 0.0f)
616+
return 0.0f;
617+
else
618+
{
619+
if (IsPositive(val))
620+
return 1.0f;
621+
else
622+
return -1.0f;
623+
}
624+
}
625+
626+
/// <summary>
627+
/// 計算旋轉的點 (JCS_VECTOR2F)
628+
/// </summary>
629+
/// <param name="point"> 我們要計算的"點" </param>
630+
/// <param name="cos"> Cos 的角度 "Cos(angle)" </param>
631+
/// <param name="sin"> Sin 的角度 "Sin(angle)" </param>
632+
/// <param name="origin"> 以這個"點"為中心旋轉 </param>
633+
public static Vector3 RotatePointX(
634+
Vector3 point, float cos, float sin, Vector3 origin)
635+
{
636+
return new Vector3(
637+
point.x,
638+
origin.z + ((point.z - origin.z) * cos) - ((point.y - origin.y) * sin),
639+
origin.y + ((point.z - origin.z) * sin) + ((point.y - origin.y) * cos));
640+
}
641+
public static Vector3 RotatePointZ(
642+
Vector3 point, float cos, float sin, Vector3 origin)
643+
{
644+
return new Vector3(
645+
origin.x + ((point.x - origin.x) * cos) - ((point.y - origin.y) * sin),
646+
origin.y + ((point.x - origin.x) * sin) + ((point.y - origin.y) * cos),
647+
point.z);
648+
}
649+
public static Vector3 RotatePointY(
650+
Vector3 point, float cos, float sin, Vector3 origin)
651+
{
652+
return new Vector3(
653+
origin.x + ((point.x - origin.x) * cos) - ((point.z - origin.z) * sin),
654+
point.y,
655+
origin.z + ((point.x - origin.x) * sin) + ((point.z - origin.z) * cos));
656+
}
657+
658+
/// <summary>
659+
/// Return a new position after rotate around the pivot.
660+
///
661+
/// Source: https://discussions.unity.com/t/rotate-a-vector-around-a-certain-point/81225/2
662+
/// </summary>
663+
/// <param name="point"> The outer point. </param>
664+
/// <param name="pivot"> The pivot point. </param>
665+
/// <param name="angles"> Angle to rotate. </param>
666+
public static Vector3 RotatePointAround(Vector3 point, Vector3 pivot, Vector3 angles)
667+
{
668+
Vector3 dir = point - pivot;
669+
dir = Quaternion.Euler(angles) * dir;
670+
point = dir + pivot;
671+
return point;
672+
}
673+
674+
/// <summary>
675+
/// 計算旋轉的點 (JCS_VECTOR2F)
676+
/// </summary>
677+
/// <param name="point"> 我們要計算的"點" </param>
678+
/// <param name="cos"> Cos 的角度 "Cos(angle)" </param>
679+
/// <param name="sin"> Sin 的角度 "Sin(angle)" </param>
680+
/// <param name="origin"> 以這個"點"為中心旋轉 </param>
681+
public static Vector3 RotatePointX(Vector3 point, Vector3 origin, float angle)
682+
{
683+
return new Vector3(
684+
point.x,
685+
origin.y + ((Mathf.Cos(angle) * (point.y - origin.y))) - (Mathf.Sin(angle) * (point.z - origin.z)),
686+
origin.z + ((Mathf.Sin(angle) * (point.y - origin.y))) + (Mathf.Cos(angle) * (point.z - origin.z)));
687+
}
688+
public static Vector3 RotatePointY(Vector3 point, Vector3 origin, float angle)
689+
{
690+
return new Vector3(
691+
origin.x + ((Mathf.Cos(angle) * (point.x - origin.x))) - (Mathf.Sin(angle) * (point.z - origin.z)),
692+
point.y,
693+
origin.z + ((Mathf.Sin(angle) * (point.x - origin.x))) + (Mathf.Cos(angle) * (point.z - origin.z)));
694+
}
695+
public static Vector3 RotatePointZ(Vector3 point, Vector3 origin, float angle)
696+
{
697+
return new Vector3(
698+
origin.x + ((Mathf.Cos(angle) * (point.x - origin.x))) - (Mathf.Sin(angle) * (point.y - origin.y)),
699+
origin.y + ((Mathf.Sin(angle) * (point.x - origin.x))) + (Mathf.Cos(angle) * (point.y - origin.y)),
700+
point.z);
701+
}
702+
622703
/// <summary>
623704
/// Find the point on the circle line base on the degree. (x-axis)
624705
/// </summary>
@@ -701,75 +782,5 @@ public static Vector3 CirclePosition(Vector3 origin, float deg, float radius, Ve
701782

702783
return circlePos;
703784
}
704-
705-
/// <summary>
706-
/// Truncate float number.
707-
///
708-
/// If 'digits'=2 and 'value'=1.345698F:
709-
/// 1.345698F => 1.34
710-
///
711-
/// If 'digits'=2 and 'value'=1.300000F:
712-
/// 1.300000F => 1.30
713-
///
714-
/// SOURCE(jenchieh): https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8092fd2-1080-416c-8ae1-2bad8c013a21/how-to-round-off-a-float-to-2-decimal-places?forum=csharpgeneral
715-
/// </summary>
716-
/// <param name="val"> Value to do truncate. </param>
717-
/// <param name="digits"> Target shown digit. </param>
718-
/// <returns> Result value. </returns>
719-
public static float Truncate(float val, int digits)
720-
{
721-
double mult = Math.Pow(10.0, digits);
722-
double result = Math.Truncate(mult * val) / mult;
723-
return (float)result;
724-
}
725-
726-
/// <summary>
727-
/// Find the greatest common factor.
728-
/// 最大公因數.
729-
///
730-
/// SOURCE: https://stackoverflow.com/questions/18541832/c-sharp-find-the-greatest-common-divisor
731-
/// AUTHOR: Drew Noakes
732-
/// </summary>
733-
/// <param name="a"> number a. </param>
734-
/// <param name="b"> number b. </param>
735-
/// <returns>
736-
/// greatest common factor for 'a' nd 'b'.
737-
/// </returns>
738-
public static int GCD(int a, int b)
739-
{
740-
while (a != 0 && b != 0)
741-
{
742-
if (a > b)
743-
a %= b;
744-
else
745-
b %= a;
746-
}
747-
748-
return (a == 0) ? b : a;
749-
}
750-
751-
/// <summary>
752-
/// Return the positive/negative 1 sign from VAL.
753-
///
754-
/// If the value is 0 then return 0.
755-
/// </summary>
756-
/// <param name="val"> Value you want to idenfity. </param>
757-
/// <returns>
758-
/// Return 0, if the VAL can't be identify.
759-
/// Return 1, if the VAL is positive value.
760-
/// Return -1, if the VAL is negative value.
761-
/// </returns>
762-
public static float GetSign(float val)
763-
{
764-
if (val == 0.0f)
765-
return 0.0f;
766-
else
767-
{
768-
if (IsPositive(val))
769-
return 1.0f;
770-
else
771-
return -1.0f;
772-
}
773-
}
774785
}
775786
}

docs/ScriptReference/Util/JCS_Mathf.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ Math library class.
2020
| FindMiddleIndex | Returns the center of the array length. |
2121
| OverFlowIndex | Returns index that are overflow within the array length. |
2222
| ApproachTo | Make value approach to the target value by increment/decrement one. |
23-
| RotatePointX | RotatePoint by x axis |
24-
| RotatePointY | Rotate point by y axis. |
25-
| RotatePointZ | Rotate point by z axis. |
2623
| IsInfinity | Check if the value is infinity? |
2724
| Min | Returns the mininum value. |
2825
| Max | Returns the maxinum value. |
@@ -40,9 +37,13 @@ Math library class.
4037
| Cos | Cosine in degree. |
4138
| Sin | Sine in degree. |
4239
| Tan | Tangent in degree. |
43-
| CirclePositionX | Find the point on the circle line base on the degree. (x-axis) |
44-
| CirclePositionY | Find the point on the circle line base on the degree. (y-axis) |
45-
| CirclePositionZ | Find the point on the circle line base on the degree. (z-axis) |
4640
| Truncate | Truncate a decimal number. |
4741
| GCD | Find the greatest common factor. |
4842
| GetSign | Return the positive/negative 1 sign from VAL. |
43+
| RotatePointAround | Return a new position after rotate around the pivot. |
44+
| RotatePointX | RotatePoint by x axis |
45+
| RotatePointY | Rotate point by y axis. |
46+
| RotatePointZ | Rotate point by z axis. |
47+
| CirclePositionX | Find the point on the circle line base on the degree. (x-axis) |
48+
| CirclePositionY | Find the point on the circle line base on the degree. (y-axis) |
49+
| CirclePositionZ | Find the point on the circle line base on the degree. (z-axis) |

0 commit comments

Comments
 (0)