用java判断是否为正方形,如何在Java中查找点是否位于正方形内?

I want to find whether a point lies inside a square or not. I have been given end coordinates of a diagonal of a square and I have found out the other diagonal's end coordinates.

One method I could think of it was to solve it by using property of circumcircle. I have included all the points inside the circumcircle of the square but I cannot think of a method to exclude the extra points.

The edges are not parallel to any coordinate axis.

Is there any method to do it in java?

Heres my part of code:

if(distance(x1,y1,x2,y2,j,i)<=dd)

A[i][j]=1;

else

A[i][j]=0;

x1,y1,x2,y2 are the coordinates of the diagonal of the square and dd is the length of diagonal and i,j are the coordinates greater than 0 and less than 100.

I am using array to store the points satisfying the condition.

解决方案

There's no built-in method to do it in Java, but it's simple math.

The equation of a line is:

a * x + b * y = c1 (1)

The equation of a line parallel to this is:

a * x + b * y = c2 (2)

The equation of two lines perpendicular to these are:

-b * x + a * y = c3 (3)

-b * x + a * y = c4 (4)

These are the equations of the four edges of the square.

Determine the coefficients of the equations above (a, b, c1..c4) for your square.

The point is inside the square iff both of the following conditions are true:

min(c1, c2) <= a * x + b*y <= max(c1, c2)

min(c3, c4) <= -b * x + a * y <= max(c3, c4)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值