java给定四个点判断正方形_判断四个点能否组成正方形&&一个点绕另一个的点旋转...

判断四个点组成正方形。

三个条件:

1.四个边相等且长度不为0

(先把点排序,再计算)

2.有一个直角(根据3个点组成的两个向量点乘为0)

struct point

{

double x, y;

} a[4];

bool cmp(point a, point b)

{

if (a.x != b.x)

return a.x < b.x; //如果,横坐标不相等,所有点按横坐标升序排列

return a.y < b.y;//如果横坐标相等,所有点按纵坐标升序排列

}

double TwoPointDistance(point a, point b)//计算两点之间的距离

{

return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));

}

bool IsRightAngle(point a, point b, point c)//判断是否为直角

{

double x;

x = (a.x - b.x)* (a.x - c.x) + (a.y - b.y)*(a.y - c.y);

if (x == 0)

return 1;

else

return 0;

}

int Is_Square()

{

sort(a,a+4,cmp);

double s1, s2, s3, s4;

s1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以采用射线法判断曲线在正方形中的走向。 具体步骤如下: 1. 获取正方形四个坐标,分别为A、B、C、D。 2. 将曲线的所有按照横坐标排序,找到最左边和最右边的,分别为P1和P2。 3. 将P1和P2连成一条直线,作为射线的方向。 4. 将射线从正方形的左侧边界向右侧延伸,统计射线与曲线的交个数,记为count1。 5. 将射线从正方形的右侧边界向左侧延伸,统计射线与曲线的交个数,记为count2。 6. 如果count1和count2均为奇数,则曲线是从左侧进入正方形,从右侧出去;如果count1和count2均为偶数,则曲线是从右侧进入正方形,从左侧出去。 7. 将射线从正方形的上侧边界向下延伸,统计射线与曲线的交个数,记为count3。 8. 将射线从正方形的下侧边界向上延伸,统计射线与曲线的交个数,记为count4。 9. 如果count3和count4均为奇数,则曲线是从下侧进入正方形,从上侧出去;如果count3和count4均为偶数,则曲线是从上侧进入正方形,从下侧出去。 10. 根据以上统计结果,可以判断曲线在正方形中的走向。 Java代码实现如下: ``` public static int countIntersection(Point p1, Point p2, List<Point> curve) { int count = 0; for (int i = 0; i < curve.size() - 1; i++) { Point p3 = curve.get(i); Point p4 = curve.get(i + 1); if (isIntersect(p1, p2, p3, p4)) { count++; } } return count; } public static boolean isIntersect(Point p1, Point p2, Point p3, Point p4) { double d1 = direction(p3, p4, p1); double d2 = direction(p3, p4, p2); double d3 = direction(p1, p2, p3); double d4 = direction(p1, p2, p4); if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0))) { return true; } else if (d1 == 0 && onSegment(p3, p4, p1)) { return true; } else if (d2 == 0 && onSegment(p3, p4, p2)) { return true; } else if (d3 == 0 && onSegment(p1, p2, p3)) { return true; } else if (d4 == 0 && onSegment(p1, p2, p4)) { return true; } return false; } public static double direction(Point p1, Point p2, Point p3) { return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y); } public static boolean onSegment(Point p1, Point p2, Point p3) { return Math.min(p1.x, p2.x) <= p3.x && p3.x <= Math.max(p1.x, p2.x) && Math.min(p1.y, p2.y) <= p3.y && p3.y <= Math.max(p1.y, p2.y); } public static String getCurveDirection(Point upperRight, Point lowerLeft, List<Point> curve) { Point A = new Point(lowerLeft.x, upperRight.y); Point B = new Point(upperRight.x, upperRight.y); Point C = new Point(upperRight.x, lowerLeft.y); Point D = new Point(lowerLeft.x, lowerLeft.y); Point P1 = curve.get(0); Point P2 = curve.get(curve.size() - 1); int count1 = countIntersection(A, B, curve); int count2 = countIntersection(B, A, curve); int count3 = countIntersection(C, D, curve); int count4 = countIntersection(D, C, curve); if (count1 % 2 == 1 && count2 % 2 == 1) { return "Left in, right out"; } else if (count1 % 2 == 0 && count2 % 2 == 0) { return "Right in, left out"; } else if (count3 % 2 == 1 && count4 % 2 == 1) { return "Down in, up out"; } else if (count3 % 2 == 0 && count4 % 2 == 0) { return "Up in, down out"; } else { return "Unknown"; } } ``` 其中,countIntersection方法用于统计射线与曲线的交个数;isIntersect方法用于判断两条线段是否相交;direction方法用于计算向量的方向;onSegment方法用于判断是否在线段上;getCurveDirection方法是主方法,用于判断曲线的走向。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值