/// <summary> /// 是否与...平行 /// </summary> /// <param name="line"></param> /// <returns></returns> public bool IsParallel(TLine line) { if (b == 0 && line.b == 0 || b != 0 && line.b != 0 && k == line.k) { return(true); } return(false); }
public Vector2 CrossWith(TLine line) { if (IsParallel(line)) { return(new Vector2(float.NaN, float.NaN)); } float d = line.a; float e = line.b; float f = line.c; float x = (b * f - c * e) / (a * e - b * d); float y = (c * d - a * f) / (a * e - b * d); Vector2 crossPoint = new Vector2(x, y); return(crossPoint); }
/// <summary> /// 如果超出边界,按照Pos到Center线段与矩形边框交叉点,作为返回值 /// </summary> /// <param name="pos"></param> /// <returns></returns> public Vector2 MakePosInside(Vector2 pos) { TLine centerToPos = new TLine(center, pos); if (pos.x < left) { TLine line = new TLine(new Vector2(left, top), new Vector2(left, bottom)); Vector2 crsp = line.CrossWith(centerToPos); if (crsp.y <= top && crsp.y >= bottom) { pos.x = left; pos.y = crsp.y; } } else if (pos.x > right) { TLine line = new TLine(new Vector2(right, top), new Vector2(right, bottom)); Vector2 crsp = line.CrossWith(centerToPos); if (crsp.y <= top && crsp.y >= bottom) { pos.x = right; pos.y = crsp.y; } } if (pos.y < bottom) { TLine line = new TLine(new Vector2(left, bottom), new Vector2(right, bottom)); Vector2 crsp = line.CrossWith(centerToPos); if (crsp.x <= right && crsp.x >= left) { pos.x = crsp.x; pos.y = bottom; } } else if (pos.y > top) { TLine line = new TLine(new Vector2(left, top), new Vector2(right, top)); Vector2 crsp = line.CrossWith(centerToPos); if (crsp.x <= right && crsp.x >= left) { pos.x = crsp.x; pos.y = top; } } return(pos); }