简体   繁体   English

XY平面中点的计算

[英]calculation of points in a XY plane

#include <stdio.h>

int main(double x, double y, double x1, double y1, double x2, double y2)
{
    // First corner (botton left) of the rectangle
    printf("Choose x and y for the first corner that the rectangle should start:\n");
    scanf("%lf%lf", &x1, &y1);

    // Opposite corner(top right) that should make the rectangle possible 
    printf("Choose x and y for the first corner that the rectangle should end:\n");
    scanf("%lf%lf", &x2, &y2);

    // The position of the point that should be checked
    printf("Choose the x and y that should be checked:\n");
    scanf("%lf%lf", &x, &y);
    if (x1 < x < x2 && y1 < y < y2)
    {
        return 5;
    }
    else if (x1 == x && x == x2 && y1 == y && y == y2)
    {
        return 3;
    }
    else
    {
        return 0;
    }

    system("pause");

}

I have got a small problem with the calculation. 我的计算有一个小问题。

I'm trying to make the program tell me if one point is inside the rectangle, on the edge or outside but I always get the result as 5 even though it's not inside the rectangle. 我试图使程序告诉我一点是否在矩形内部,边缘还是外部,但是即使不在矩形内部,我也总是得到5的结果。

Also, I am not sure if I have missed to mention the "double x, double y,..." somewhere or if I only should write like I did the scanf statment? 另外,我不确定是否错过某个地方的“ double x,double y,...”,还是只写得像scanf语句一样?

You should try to replace this test 您应该尝试替换此测试

if (x1 < x < x2 && y1 < y < y2)

with

if (x1 < x && x < x2 && y1 < y && y < y2)

Regarding your 2nd test, it will never be true unless x1=x2 and y1=y2, ie your rectangle actually is a point. 关于第二项测试,除非x1 = x2和y1 = y2,否则它永远不会成立,即您的矩形实际上是一个点。 Replace with 用。。。来代替

else if ( (x1 == x || x == x2) && (y1 == y || y == y2))

Various problems 各种问题


  • As @StephaneM identified, the x1 < x < x2 is incorrect. @StephaneM所标识, x1 < x < x2不正确。

     // if (x1 < x < x2 && y1 < y < y2) if (x1 < x && x < x2 && y1 < y && y < y2) 

x1 < x < x2 tests if x1 < x , which results in 0 or 1. Then 0_or_1 < x2 is tested. x1 < x < x2测试x1 < x结果是否为0或1。然后测试0_or_1 < x2 Not what OP needs. 不是OP所需要的。


  • Unusual and certainly incorrect main() function signature 异常且肯定不正确的main()函数签名

Define the double variables after the function header. 在函数标题之后定义double变量。

// int main(double x, double y, double x1, double y1, double x2, double y2) {
int main(void) {
  double x, y, x1, y1, x2, y2;

  • Incorrect "on the edge" test 错误的“边缘”测试

x1 == x && x == x2 && y1 == y && y == y2 is only true when the rectangle and point are all a single point. x1 == x && x == x2 && y1 == y && y == y2仅在矩形和点都是单个点时才为真。

Instead, take advantage that the point is not inside. 而是利用该点不在内部的优势。

if (x1 < x && x < x2 && y1 < y && y < y2) {
    return 5; // inside
} else if (x1 <= x && x <= x2 && y1 <= y && y <= y2) {  // include = in each compare
    return 3; // on edge
} else {
    return 0; // outside
}

Code may also want to exchange x1,x2 should x2 < x1 . 代码可能还希望交换x1,x2如果x2 < x1 Same for y1,y2 . y1,y2相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM