如何确定点是否位于矩形内?

时间:2012-11-06 16:46:00

标签: c points rectangles

我正在进行一项任务,我必须为用户创建一个程序来输入矩形的坐标。

此程序旨在成为结构中的结构。

如果无效,我必须输出错误消息,让用户无限期地再试一次,直到用户做对了。

程序应该反复询问用户点的坐标,当用户分别为x和y输入0和0时,程序将退出。

程序必须说明该点是在矩形的内部还是外部,并打印出内部的。我还需要弄清楚主要功能放在哪里,以及放入什么内容。 感谢。

这是我的代码:

#include <stdio.h>
typedef struct
{
int x;
int y;
} point_t;

typedef struct
{
point_t upper_left;
point_t lower_right;
} rectangle_t;

int is_inside (point_t* pPoint, rectangle_t* pRect)
{
return ((pPoint->x >= pRect->upper_left.x ) &&
(pPoint->x <= pRect->lower_right.x) &&
(pPoint->y >= pRect->upper_left.y ) &&
(pPoint->y <= pRect->lower_right.y));

}

point_t get_point(char* prompt)
{
point_t pt;
printf("Given a rectangle with a side parallel to the x axis and a series of points on          the xy plane this program will say where each point lies in relation to the rectangle. It   considers a point on the boundary of the rectangle to be inside the rectangle\n");
printf ("Enter coordinates for the upper left corner\n");
printf ("X: ");
scanf ("%d", &pt.x);
printf ("Y: ");
scanf ("%d", &pt.y);

return pt;
}

rectangle_t get_rect(char* prompt)
{
rectangle_t rect;
printf (prompt);
rect.upper_left = get_point("Upper left corner: \n");
rect.lower_right = get_point("Lower right corner: \n");

return rect;
}
int main(){
/* calls goes here */
return 0;
}

编辑:我添加了main,但我不知道如何调用函数。

1 个答案:

答案 0 :(得分:0)

我建议你修改一下你的风格,并应用这样的东西:

  1. 在main之前声明函数的原型。
  2. 我认为你不需要使用指针(引用传递 函数)对于你的大多数函数is_inside,因为它们不会修改 价值观。
  3. 将矩形点声明为点类型(实际上您将它们用作点)。
  4. 要进行调用,只需填写main中函数的参数即可。 例如:

    /* Headers, libs, definitions, etc must be here */
    /* Prototypes must be here */
    
    int main (void)
    {
        /* Variables declarations */
        int test_point;
        point_t point;
        rectanle_t rectangle;
    
        /* Functions calls... */
        /* holder = function_name( argument1, argument2 ); */
    
        test_point = is_inside( &point, &rectangle ); 
    
    
    }
    /*Function Declarations must be here*/
    
相关问题