public void normalize() { LDVector2 normalized = this.normalized(); this.xp = normalized.xp; this.yp = normalized.yp; }
public float distanceToLine(LDVector2 point, LDVector2 direction) { if (direction.isNull()) { return(distanceToPoint(point)); } return((float)Math.Sin(Math.Acos(dotProduct((this - point), direction) / (this - point).length())) * (this - point).length()); }
/** * v1からv2への反時計回りの角度 (0~2pi) */ public static double getAngle(LDVector2 v1, LDVector2 v2) { double q1 = Math.Atan2(v1.y(), v1.x()); double q2 = Math.Atan2(v2.y(), v2.x()); double ret = q2 - q1;//v2の角度からv1 の角度を引く if (ret < 0) { ret += 2 * PI; //0より小さければ、360度足す } return(ret); }
public LDPoint inverseTransformOneMinusT(float x, float y) { //Cubismから移植。 LDVector2 v0 = new LDVector2(new LDPoint(x, y) - m_p2); LDVector2 v1 = new LDVector2(m_p1 - m_p2); LDVector2 v2 = new LDVector2(m_p0 - m_p2); double det = v1.x() * v2.y() - v1.y() * v2.x(); double tx = (v2.y() * v0.x() - v2.x() * v0.y()) / det; double ty = (-v1.y() * v0.x() + v1.x() * v0.y()) / det; return(new LDPoint(1 - (float)tx, 1 - (float)ty)); }
public LDPoint inverseTransformOneMinusT(float x, float y) { //Cubismから移植。 LDVector2 v0 = new LDVector2(new LDPoint(x, y) - m_p2); LDVector2 v1 = new LDVector2(m_p1 - m_p2); LDVector2 v2 = new LDVector2(m_p0 - m_p2); double det = v1.x() * v2.y() - v1.y() * v2.x(); double tx = (v2.y() * v0.x() - v2.x() * v0.y()) / det; double ty = (-v1.y() * v0.x() + v1.x() * v0.y()) / det; return new LDPoint(1 - (float)tx, 1 - (float)ty); }
public bool isHit(LDPointList form, LDPoint p, float hitRange) { LDLine l = this.toLine(form); LDPoint startPt = l.p1(); LDPoint endPt = l.p2(); //端点から一定の距離内だったら当たり if (PointUtil.isHit(startPt, p, hitRange)) { return(true); } if (PointUtil.isHit(endPt, p, hitRange)) { return(true); } //点が一直線上にないか確認 if (TriangleUtil.isTriangle(startPt, endPt, p)) { //鈍角三角形なら判定外 if (TriangleUtil.isObtuseAngle(startPt, endPt, p)) { return(false); } //三角形の面積を算出して、その底面で割れば高さ=線と点の距離 float distance = TriangleUtil.getTriangleHeight(startPt, endPt, p); if (distance <= hitRange) { return(true); } return(false); } //一直線上にあるが線分外にあるか判定 LDVector2 ab = new LDVector2(endPt - startPt); LDVector2 ap = new LDVector2(p - startPt); LDVector2 bp = new LDVector2(p - endPt); float omega = 0.0001f;//NOTE:誤差の基準値 かなり適当に指定 if (ap.length() + bp.length() > ab.length() + omega) { return(false); } return(true); }
public LDPoint inverseTransform(float x, float y) { //三角形の1点を基準点とする(この場合index0番) //基準点から2辺に向かうベクトルをV1,V2とし、(x,y)へ向かうベクトルをV0とする //V0 = s*V1 + t*V2 //とするとき //(s,t) = ((V1*V2)の逆行列) * V0 //Cubismから移植。 LDVector2 v0 = new LDVector2(new LDPoint(x, y) - m_p0); LDVector2 v1 = new LDVector2(m_p1 - m_p0); LDVector2 v2 = new LDVector2(m_p2 - m_p0); double det = v1.x() * v2.y() - v1.y() * v2.x(); double tx = (v2.y() * v0.x() - v2.x() * v0.y()) / det; double ty = (-v1.y() * v0.x() + v1.x() * v0.y()) / det; return new LDPoint((float)tx, (float)ty); }
public LDPoint inverseTransform(float x, float y) { //三角形の1点を基準点とする(この場合index0番) //基準点から2辺に向かうベクトルをV1,V2とし、(x,y)へ向かうベクトルをV0とする //V0 = s*V1 + t*V2 //とするとき //(s,t) = ((V1*V2)の逆行列) * V0 //Cubismから移植。 LDVector2 v0 = new LDVector2(new LDPoint(x, y) - m_p0); LDVector2 v1 = new LDVector2(m_p1 - m_p0); LDVector2 v2 = new LDVector2(m_p2 - m_p0); double det = v1.x() * v2.y() - v1.y() * v2.x(); double tx = (v2.y() * v0.x() - v2.x() * v0.y()) / det; double ty = (-v1.y() * v0.x() + v1.x() * v0.y()) / det; return(new LDPoint((float)tx, (float)ty)); }
public bool isHit(LDPointList form , LDPoint p, float hitRange) { LDLine l = this.toLine(form); LDPoint startPt = l.p1(); LDPoint endPt = l.p2(); //端点から一定の距離内だったら当たり if (PointUtil.isHit(startPt, p, hitRange)) return true; if (PointUtil.isHit(endPt, p, hitRange)) return true; //点が一直線上にないか確認 if (TriangleUtil.isTriangle(startPt, endPt, p)) { //鈍角三角形なら判定外 if (TriangleUtil.isObtuseAngle(startPt, endPt, p)) { return false; } //三角形の面積を算出して、その底面で割れば高さ=線と点の距離 float distance = TriangleUtil.getTriangleHeight(startPt, endPt, p); if (distance <= hitRange) { return true; } return false; } //一直線上にあるが線分外にあるか判定 LDVector2 ab = new LDVector2(endPt - startPt); LDVector2 ap = new LDVector2(p - startPt); LDVector2 bp = new LDVector2(p - endPt); float omega = 0.0001f;//NOTE:誤差の基準値 かなり適当に指定 if (ap.length() + bp.length() > ab.length() + omega) { return false; } return true; }
/// <summary> /// 未実装 /// </summary> /// <param name="point"></param> /// <returns></returns> public float distanceToPoint(LDVector2 point) { throw new NotImplementedException(); }
public static float dotProduct(LDVector2 v1, LDVector2 v2) { return v1.xp * v2.xp + v1.yp * v2.yp; }
public bool uFuzzyCompare(LDVector2 v1, LDVector2 v2) { return MathFunctions.uFuzzyCompare(v1.xp, v2.xp) && MathFunctions.uFuzzyCompare(v1.yp, v2.yp); }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> public float distanceToPoint(LDVector2 point) { return (this - point).length(); }
public float distanceToLine(LDVector2 point, LDVector2 direction) { if (direction.isNull()) return distanceToPoint(point); return (float)Math.Sin(Math.Acos(dotProduct((this-point), direction) / (this-point).length())) * (this - point).length(); }
public float distanceToLine(LDVector2 point, LDVector2 direction) { throw new NotImplementedException(); }
//外周の頂点インデックスを取得。yが一番小さい点から時計周りで取得。 public List <int> getOutlinePointIndices(LDPointList points) { List <int> result = new List <int>(); // 外周をたどる。始点に戻ったら終了。 int startIndex = math.PointUtil.findMinYPointIndex(points); Debug.Assert(startIndex >= 0); result.Add(startIndex); int lastIndex = -1; int currentIndex = startIndex; for (int i = 0; i < points.length(); ++i) { //現在の頂点と接続される点一覧を取得し、その中から進行方向に対してもっとも左側に位置するものを取得 List <int> related = getRelatedPointIndices(currentIndex); LDPoint lastPoint; LDPoint currentPoint = points[currentIndex]; if (lastIndex == -1) { lastPoint = currentPoint - new LDPoint(0, 1); } else { lastPoint = points[lastIndex]; } int nextIndex = -1; double minAngle = 360; foreach (var targetIndex in related) { LDPoint targetPoint = points[targetIndex]; if (targetIndex == lastIndex) { continue; } LDVector2 v1 = new LDVector2(lastPoint - currentPoint); LDVector2 v2 = new LDVector2(targetPoint - currentPoint); double angle = LDMathUtil.getAngle(v2, v1); if (angle < minAngle) { minAngle = angle; nextIndex = targetIndex; } } if (nextIndex == startIndex) { //一周した break; } result.Add(nextIndex); lastIndex = currentIndex; currentIndex = nextIndex; } return(result); }
public static float dotProduct(LDVector2 v1, LDVector2 v2) { return(v1.xp * v2.xp + v1.yp * v2.yp); }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> public float distanceToPoint(LDVector2 point) { return((this - point).length()); }
public bool uFuzzyCompare(LDVector2 v1, LDVector2 v2) { return(MathFunctions.uFuzzyCompare(v1.xp, v2.xp) && MathFunctions.uFuzzyCompare(v1.yp, v2.yp)); }