1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183 |
- using CommonLang.Geometry;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace CommonLang
- {
-
- public class CMath
- {
- public static int sqrt(int i)
- {
- int l = 0;
- for (int k = 0x100000; k != 0; k >>= 2)
- {
- int j = l + k;
- l >>= 1;
- if (j <= i)
- {
- i -= j;
- l += k;
- }
- }
- return l;
- }
-
-
-
-
-
-
-
-
- static public int cycNum(int value, int d, int max)
- {
- value += d;
- return (value >= 0) ? (value % max) : ((max + value % max) % max);
- }
-
-
-
-
-
-
- static public int cycNum(int value, int max)
- {
- return (value >= 0) ? (value % max) : ((max + value % max) % max);
- }
-
-
-
-
-
-
- static public int cycMod(int value, int div)
- {
- return (value / div) + (value < 0 ? -1 : 0);
- }
-
-
-
-
-
- static public int getDirect(int value)
- {
- return value == 0 ? 0 : (value > 0 ? 1 : -1);
- }
-
-
-
-
-
- static public float getDirect(float value)
- {
- return value == 0 ? 0 : (value > 0 ? 1 : -1);
- }
-
-
-
-
-
-
- static public int roundMod(int value, int div)
- {
- return (value / div) + (value % div == 0 ? 0 : (1 * getDirect(value)));
- }
-
-
-
-
-
-
- static public int roundMod(float value, float div)
- {
- return (int)(value / div) + (value % div == 0 ? 0 : (1 * getDirect((int)value)));
- }
-
-
-
-
-
-
- static public float getDistanceSpeedInTime(float speed, float interval_ms)
- {
- float rate = interval_ms / 1000.0f;
- return speed * rate;
- }
-
-
-
-
-
-
-
-
- public static float getDistance(float x1, float y1, float x2, float y2)
- {
- float r1 = x1 - x2;
- float r2 = y1 - y2;
- return (float)Math.Sqrt(r1 * r1 + r2 * r2);
- }
-
-
-
-
-
-
-
-
- public static float getDistanceSquare(float x1, float y1, float x2, float y2)
- {
- float r1 = x1 - x2;
- float r2 = y1 - y2;
- return r1 * r1 + r2 * r2;
- }
-
- #region _Geometry_Collision_Include_
-
-
-
-
-
-
-
-
-
-
-
- static public bool includeFanPoint(float sx, float sy, float sr, float dx, float dy, float startAngle, float endAngle)
- {
- float ddx = dx - sx;
- float ddy = dy - sy;
- float r = sr;
- if (ddx * ddx + ddy * ddy <= r * r)
- {
- float direction = OpitimizeRadians((float)Math.Atan2(ddy, ddx));
- startAngle = OpitimizeRadians(startAngle);
- endAngle = OpitimizeRadians(endAngle);
- if (endAngle < startAngle)
- {
- if (direction < endAngle)
- {
- direction += PI_MUL_2;
- }
- endAngle += PI_MUL_2;
- }
- if (direction >= startAngle && direction <= endAngle)
- {
- return true;
- }
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
- static public bool includeRoundPoint(float sx, float sy, float sr, float px, float py)
- {
- float w = sx - px;
- float h = sy - py;
- return (w * w + h * h) <= (sr * sr);
- }
-
-
-
-
-
-
-
-
-
-
- static public bool includeRound2(float sx, float sy, float sr, float dx, float dy, float dr)
- {
- float w = sx - dx;
- float h = sy - dy;
- float r = sr - dr;
- return (w * w + h * h) <= (r * r);
- }
-
-
-
-
-
-
-
-
-
-
-
- static public bool includeRectPoint(float sx1, float sy1, float sx2, float sy2, float dx, float dy)
- {
- if (sx2 < dx) return false;
- if (sx1 > dx) return false;
- if (sy2 < dy) return false;
- if (sy1 > dy) return false;
- return true;
- }
-
-
-
-
-
-
-
-
-
-
- static public bool includeRectPoint2(float sx1, float sy1, float sw, float sh, float dx, float dy)
- {
- float sx2 = sx1 + sw;
- if (sx2 < dx) return false;
- if (sx1 > dx) return false;
- float sy2 = sy1 + sh;
- if (sy2 < dy) return false;
- if (sy1 > dy) return false;
- return true;
- }
-
-
-
-
-
-
-
-
-
-
- public static bool includeEllipsePoint(float x0, float y0, float rx, float ry, float dx, float dy)
- {
- return intersectRectEllipse(dx, dy, dx, dy, x0, y0, rx, ry);
- }
-
-
-
-
-
-
-
-
-
-
-
- public static bool includeStripWidthPoint(
- float lx1, float ly1,
- float lx2, float ly2,
- float line_r,
- float dx, float dy)
- {
- CommonLang.Geometry.Vector2[] list = toStripWidthPolygon(lx1, ly1, lx2, ly2, line_r);
- return includePolygonPoint(list, dx, dy);
- }
-
-
-
-
-
-
-
- public static bool includePolygonPoint(Vector2[] list, float dx, float dy)
- {
- return CommonLang.Geometry.CollisionMath.PointInPolygon(new Vector2(dx, dy), list);
- }
- #endregion
-
- #region _Geometry_Collision_Intersect_
-
-
-
-
-
-
-
-
-
-
- static public bool intersectRound(
- float sx, float sy, float sr,
- float dx, float dy, float dr)
- {
- float w = sx - dx;
- float h = sy - dy;
- float r = sr + dr;
- return (w * w + h * h) <= (r * r);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- static public bool intersectFanRound(
- float sx, float sy, float sr,
- float dx, float dy, float dr,
- float startAngle, float endAngle)
- {
- float ddx = dx - sx;
- float ddy = dy - sy;
- float r = sr + dr;
- if (ddx * ddx + ddy * ddy <= r * r)
- {
- float direction = OpitimizeRadians((float)Math.Atan2(ddy, ddx));
- startAngle = OpitimizeRadians(startAngle);
- endAngle = OpitimizeRadians(endAngle);
- if (endAngle < startAngle)
- {
- if (direction < endAngle)
- {
- direction += PI_MUL_2;
- }
- endAngle += PI_MUL_2;
- }
- if (direction >= startAngle && direction <= endAngle)
- {
- return true;
- }
- if (intersectLineRound(sx, sy,
- sx + (float)Math.Cos(startAngle) * sr,
- sy + (float)Math.Sin(startAngle) * sr,
- dx, dy, dr))
- {
- return true;
- }
- if (intersectLineRound(sx, sy,
- sx + (float)Math.Cos(endAngle) * sr,
- sy + (float)Math.Sin(endAngle) * sr,
- dx, dy, dr))
- {
- return true;
- }
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- static public bool intersectRect(
- float sx1, float sy1, float sx2, float sy2,
- float dx1, float dy1, float dx2, float dy2)
- {
- if (sx2 < dx1) return false;
- if (sx1 > dx2) return false;
- if (sy2 < dy1) return false;
- if (sy1 > dy2) return false;
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- static public bool intersectRect2(
- float sx1, float sy1, float sw, float sh,
- float dx1, float dy1, float dw, float dh)
- {
- float sx2 = sx1 + sw;
- float dx2 = dx1 + dw;
- if (sx2 < dx1) return false;
- if (sx1 > dx2) return false;
- float sy2 = sy1 + sh;
- float dy2 = dy1 + dh;
- if (sy2 < dy1) return false;
- if (sy1 > dy2) return false;
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- static public bool intersectRectLine(
- float sx1, float sy1,
- float sx2, float sy2,
- float lx1, float ly1,
- float lx2, float ly2)
- {
- float dx1 = lx1;
- float dy1 = ly1;
- float dx2 = lx2;
- float dy2 = ly2;
- if (dx1 > dx2) CUtils.Swap(ref dx1, ref dx2);
- if (dy1 > dy2) CUtils.Swap(ref dy1, ref dy2);
- if (!intersectRect(sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2))
- {
- return false;
- }
- if (intersectLine(lx1, ly1, lx2, ly2, sx1, sy1, sx2, sy1))
- {
- return true;
- }
- if (intersectLine(lx1, ly1, lx2, ly2, sx2, sy1, sx2, sy2))
- {
- return true;
- }
- if (intersectLine(lx1, ly1, lx2, ly2, sx2, sy2, sx1, sy2))
- {
- return true;
- }
- if (intersectLine(lx1, ly1, lx2, ly2, sx1, sy2, sx1, sy1))
- {
- return true;
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
- static public bool intersectRectRound(float sx, float sy, float dx, float dy, float cx, float cy, float r)
- {
- return intersectRectEllipse(sx, sy, dx, dy, cx, cy, r, r);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool intersectRectEllipse(float xmin, float ymin, float xmax, float ymax, float x0, float y0, float rx, float ry)
- {
-
-
-
-
-
- if (includeRectPoint(xmin, ymin, xmax, ymax, x0, y0))
- return true;
-
- float x = x0, y = y0;
- if (x < xmin)
- {
- x = xmin;
- }
- else if (x > xmax)
- {
- x = xmax;
- }
- if (y < ymin)
- {
- y = ymin;
- }
- else if (y > ymax)
- {
- y = ymax;
- }
- float dx = x - x0;
- float dy = y - y0;
- dx *= dx;
- dy *= dy;
- rx *= rx;
- ry *= ry;
- dx *= ry;
- dy *= rx;
- if (dx + dy <= rx * ry)
- {
- return true;
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static public bool intersectLine(
- float p1_x, float p1_y, float p2_x, float p2_y,
- float q1_x, float q1_y, float q2_x, float q2_y)
- {
-
- float v1_x = q2_x - q1_x;
- float v1_y = q2_y - q1_y;
- float v2_x = p1_x - q1_x;
- float v2_y = p1_y - q1_y;
- float v3_x = p2_x - q1_x;
- float v3_y = p2_y - q1_y;
- if ((v1_x * v2_y - v1_y * v2_x) * (v3_x * v1_y - v3_y * v1_x) < 0)
- {
- return false;
- }
- float v5_x = p2_x - p1_x;
- float v5_y = p2_y - p1_y;
- float v6_x = q1_x - p1_x;
- float v6_y = q1_y - p1_y;
- float v7_x = q2_x - p1_x;
- float v7_y = q2_y - p1_y;
- if ((v5_x * v6_y - v5_y * v6_x) * (v7_x * v5_y - v7_y * v5_x) < 0)
- {
- return false;
- }
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static public bool intersectLine(
- float p1_x, float p1_y, float p2_x, float p2_y,
- float q1_x, float q1_y, float q2_x, float q2_y,
- bool ignore_touch = true)
- {
-
- float v1_x = q2_x - q1_x;
- float v1_y = q2_y - q1_y;
- float v2_x = p1_x - q1_x;
- float v2_y = p1_y - q1_y;
- float v3_x = p2_x - q1_x;
- float v3_y = p2_y - q1_y;
- float f = (v1_x * v2_y - v1_y * v2_x) * (v3_x * v1_y - v3_y * v1_x);
- if (ignore_touch ? f <= 0 : f < 0)
- {
- return false;
- }
- float v5_x = p2_x - p1_x;
- float v5_y = p2_y - p1_y;
- float v6_x = q1_x - p1_x;
- float v6_y = q1_y - p1_y;
- float v7_x = q2_x - p1_x;
- float v7_y = q2_y - p1_y;
- float s = (v5_x * v6_y - v5_y * v6_x) * (v7_x * v5_y - v7_y * v5_x);
- if (ignore_touch ? s <= 0 : s < 0)
- {
- return false;
- }
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
- public static bool intersectLineRound(
- float lx1, float ly1,
- float lx2, float ly2,
- float cx, float cy,
- float Radius)
- {
- return CommonLang.Geometry.CollisionMath.CircleLineCollide(new Vector2(cx, cy), Radius, new Vector2(lx1, ly1), new Vector2(lx2, ly2));
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool intersectRoundStripCapsule(
- float cx, float cy, float r,
- float lx1, float ly1,
- float lx2, float ly2,
- float line_r)
- {
- return CommonLang.Geometry.CollisionMath.CircleLineCollide(new Vector2(cx, cy), line_r + r, new Vector2(lx1, ly1), new Vector2(lx2, ly2));
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool intersectRoundStripWidth(
- float cx, float cy, float r,
- float lx1, float ly1,
- float lx2, float ly2,
- float line_r)
- {
- CommonLang.Geometry.Vector2 sp = new CommonLang.Geometry.Vector2(lx1, ly1);
- CommonLang.Geometry.Vector2 dp = new CommonLang.Geometry.Vector2(lx2, ly2);
- if (CommonLang.Geometry.CollisionMath.CircleLineCollide(new Vector2(cx, cy), line_r + r, sp, dp))
- {
- float angle = (float)Math.Atan2(ly1 - ly2, lx1 - lx2);
- CommonLang.Geometry.Vector2 sl = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle + CMath.PI_DIV_2, line_r);
- CommonLang.Geometry.Vector2 sr = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle - CMath.PI_DIV_2, line_r);
- CommonLang.Geometry.Vector2 dl = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle + CMath.PI_DIV_2, line_r);
- CommonLang.Geometry.Vector2 dr = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle - CMath.PI_DIV_2, line_r);
- return CMath.intersectRoundPolygon(cx, cy, r, new CommonLang.Geometry.Vector2[] { sl, sr, dr, dl, });
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool intersectRectStripWidth(
- float sx1, float sy1,
- float sx2, float sy2,
- float lx1, float ly1,
- float lx2, float ly2,
- float line_r)
- {
- if (CMath.intersectRect(sx1, sy1, sx2, sy2,
- Math.Min(lx1, lx2),
- Math.Min(ly1, ly2),
- Math.Max(lx1, lx2),
- Math.Max(ly1, ly2)))
- {
- CommonLang.Geometry.Vector2 sp = new CommonLang.Geometry.Vector2(lx1, ly1);
- CommonLang.Geometry.Vector2 dp = new CommonLang.Geometry.Vector2(lx2, ly2);
- float angle = (float)Math.Atan2(ly1 - ly2, lx1 - lx2);
- CommonLang.Geometry.Vector2 p0 = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle + CMath.PI_DIV_2, line_r);
- CommonLang.Geometry.Vector2 p1 = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle - CMath.PI_DIV_2, line_r);
- CommonLang.Geometry.Vector2 p2 = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle - CMath.PI_DIV_2, line_r);
- CommonLang.Geometry.Vector2 p3 = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle + CMath.PI_DIV_2, line_r);
- if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p0.X, p0.Y, p1.X, p1.Y)) return true;
- if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p1.X, p1.Y, p2.X, p2.Y)) return true;
- if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p2.X, p2.Y, p3.X, p3.Y)) return true;
- if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p3.X, p3.Y, p0.X, p0.Y)) return true;
- }
- return false;
- }
-
-
-
-
-
-
-
-
- public static bool intersectRoundPolygon(float cx, float cy, float r, Vector2[] list)
- {
- if (list.Length == 1)
- {
- return CMath.includeRoundPoint(cx, cy, r, list[0].X, list[0].Y);
- }
- Vector2 center = new Vector2(cx, cy);
- if (CollisionMath.PointInPolygon(center, list))
- {
- return true;
- }
- for (int i = 0; i < list.Length - 1; ++i)
- {
- if (CollisionMath.CircleLineCollide(center, r, list[i], list[i + 1]))
- return true;
- }
- if (CollisionMath.CircleLineCollide(center, r, list[list.Length - 1], list[0]))
- {
- return true;
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
- public static bool intersectRectPolygon(float sx1, float sy1, float sx2, float sy2, Vector2[] list)
- {
- if (list.Length == 1)
- return CMath.includeRectPoint(sx1, sy1, sx2, sy2, list[0].X, list[0].Y);
- if (CollisionMath.PointInPolygon(new Vector2(
- CMath.getMiddleValue(0.5f, sx1, sx2),
- CMath.getMiddleValue(0.5f, sy1, sy2)),
- list))
- return true;
- Vector2 p = list[0];
- Vector2 q = list[list.Length - 1];
- if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p.X, p.Y, q.X, q.Y))
- return true;
- for (int i = list.Length - 2; i >= 0; --i)
- {
- p = list[i];
- q = list[i + 1];
- if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p.X, p.Y, q.X, q.Y))
- return true;
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
- public static Vector2[] toStripWidthPolygon(float lx1, float ly1, float lx2, float ly2, float line_r)
- {
- Vector2[] ret = new Vector2[4];
- CommonLang.Geometry.Vector2 sp = new CommonLang.Geometry.Vector2(lx1, ly1);
- CommonLang.Geometry.Vector2 dp = new CommonLang.Geometry.Vector2(lx2, ly2);
- float angle = (float)Math.Atan2(ly1 - ly2, lx1 - lx2);
- ret[0] = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle + CMath.PI_DIV_2, line_r);
- ret[1] = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle - CMath.PI_DIV_2, line_r);
- ret[3] = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle + CMath.PI_DIV_2, line_r);
- ret[2] = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle - CMath.PI_DIV_2, line_r);
- return ret;
- }
-
-
-
-
-
-
- public static void toBoundingBox(Vector2[] list, out Vector2 min, out Vector2 max)
- {
- min = new Vector2(float.MaxValue, float.MaxValue);
- max = new Vector2(float.MinValue, float.MinValue);
- for (int i = list.Length - 1; i >= 0; --i)
- {
- if (list[i].X < min.X) { min.X = list[i].X; }
- if (list[i].Y < min.Y) { min.Y = list[i].Y; }
- if (list[i].X > max.X) { max.X = list[i].X; }
- if (list[i].Y > max.Y) { max.Y = list[i].Y; }
- }
- }
- #endregion
-
-
-
-
-
-
-
-
- public static bool isIncludeEqual(int value, int min, int max)
- {
- return max >= value && min <= value;
- }
-
-
-
-
-
-
-
- public static bool isInclude(int value, int min, int max)
- {
- return max > value && min < value;
- }
-
-
-
-
-
-
- public static bool isInRange(int value, int max)
- {
- return value >= 0 && value < max;
- }
- static public bool IsIntersect(float sx1, float sx2, float dx1, float dx2)
- {
- if (sx2 < dx1) return false;
- if (sx1 > dx2) return false;
- return true;
- }
- static public bool IsIntersect2(float sx1, float sw, float dx1, float dw)
- {
- float sx2 = sx1 + sw;
- if (sx2 < dx1) return false;
- float dx2 = dx1 + dw;
- if (sx1 > dx2) return false;
- return true;
- }
-
-
-
-
-
-
-
- public static float getInRange(
- float value,
- float min,
- float max)
- {
- value = Math.Min(max, value);
- value = Math.Max(min, value);
- return value;
- }
-
-
-
-
-
-
-
- public static float getRate(
- float value,
- float min_v,
- float max_v)
- {
- float sw = max_v - min_v;
- float sx = value - min_v;
- return (sx / sw);
- }
-
-
-
-
-
-
-
- public static float getMiddleValue(
- float rate,
- float min,
- float max)
- {
- float v = max - min;
- return min + v * rate;
- }
-
- public static long ccNextPOT(long x)
- {
- x = x - 1;
- x = x | (x >> 1);
- x = x | (x >> 2);
- x = x | (x >> 4);
- x = x | (x >> 8);
- x = x | (x >> 16);
- return x + 1;
- }
- public const float PI_F = (float)(Math.PI);
- public const float PI_FLOAT = (float)(Math.PI);
-
-
-
- public const float PI_DIV_180 = (float)(Math.PI / 180);
-
-
-
- public const float PI_DIV_2 = (float)(Math.PI / 2);
-
-
-
- public const float PI_MUL_2 = (float)(Math.PI * 2);
-
-
-
-
-
- public static float DegreesToRadians(float degrees)
- {
- return (PI_DIV_180 * degrees);
- }
-
-
-
-
-
- public static float RadiansToDegrees(float radians)
- {
- return (radians / PI_DIV_180);
- }
- public static float ToPI(float angle)
- {
- return (angle * PI_DIV_180);
- }
- public static float To360(float angle)
- {
- return (angle / PI_DIV_180);
- }
-
-
-
-
-
- public static float OpitimizeDegrees(float degrees)
- {
- while (degrees > 360)
- {
- degrees -= 360;
- }
- while (degrees < 0)
- {
- degrees += 360;
- }
- return degrees;
- }
-
-
-
-
-
- public static float OpitimizeRadians(float radians)
- {
- while (radians > PI_MUL_2)
- {
- radians -= PI_MUL_2;
- }
- while (radians < 0)
- {
- radians += PI_MUL_2;
- }
- return radians;
- }
-
-
-
-
-
-
-
- public static bool RadiansInRange(float angle, float startAngle, float endAngle)
- {
- startAngle = OpitimizeRadians(startAngle);
- endAngle = OpitimizeRadians(endAngle);
- if (endAngle < startAngle)
- {
- if (angle < endAngle)
- {
- angle += PI_MUL_2;
- }
- endAngle += PI_MUL_2;
- }
- if (angle >= startAngle && angle <= endAngle)
- {
- return true;
- }
- return false;
- }
-
-
-
-
-
-
- public static float GetMinRadians(float src_angle, float dst_angle)
- {
- float dd = CMath.OpitimizeRadians(src_angle);
- float cd = CMath.OpitimizeRadians(dst_angle);
- float d1 = Math.Abs(dd - cd);
- if (d1 > PI_F)
- {
- return PI_MUL_2 - d1;
- }
- return d1;
- }
-
- public static void RandomPosInRound(Random rand, float x, float y, float r, ref Vector.Vector2 pos)
- {
- float angle = (float)rand.NextDouble() * CMath.PI_MUL_2;
- float distance = (float)rand.NextDouble() * r;
- pos.SetX(x + (float)Math.Cos(angle) * distance);
- pos.SetY(y + (float)Math.Sin(angle) * distance);
- }
- public static void RandomPosInRound(Random rand, float x, float y, float r, out float out_x, out float out_y)
- {
- float angle = (float)rand.NextDouble() * CMath.PI_MUL_2;
- float distance = (float)rand.NextDouble() * r;
- out_x = x + (float)Math.Cos(angle) * distance;
- out_y = y + (float)Math.Sin(angle) * distance;
- }
- public static void RandomPosInCycle(Random rand, float x, float y, float r, out float out_x, out float out_y)
- {
- float angle = (float)rand.NextDouble() * CMath.PI_MUL_2;
- out_x = x + (float)Math.Cos(angle) * r;
- out_y = y + (float)Math.Sin(angle) * r;
- }
- public static float RandomAngle(Random rand)
- {
- return (float)rand.NextDouble() * CMath.PI_MUL_2;
- }
-
- public enum PointOnLineResult : byte
- {
- Left, Right, Touch,
- }
-
-
-
-
-
-
-
-
-
-
- public static PointOnLineResult PointOnLine(float x0, float y0, float x1, float y1, float x2, float y2)
- {
- float f = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
- if (f > 0)
- return PointOnLineResult.Right;
- if (f < 0)
- return PointOnLineResult.Left;
- return PointOnLineResult.Touch;
- }
- }
- }
|