关于C#:检查圆内还是圆外的点(仅通过结构实现)

Check whether a point inside or outside a circle (implementation with structs only)

首先,我给了一个任务,仅使用结构体来实现。

我需要检查一个点是否在圆的内部/外部。

输入:点,圆心,半径的坐标。

输出:是圆内/外的点。

好吧,我需要使用距离公式d = sqrt( (x_1-x_2)^2 + (y_1 - y_2)^2 ),然后检查它是否大于/小于/等于半径。

我知道逻辑,但是使用struct的语法失败。你们可以帮我吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct {
    float x;
    float y;
}Point;

typedef struct {
    Point center;
    float radius;
}Circle;

int main()
{
    Point Coordinates;
    Coordinates.x = 0; //Is this initialization necessary?
    Coordinates.y = 0; //Is this initialization necessary?
    Circle insideCircle;
    float distance;
    printf("Please enter the coordinates of your point:");
    scanf("%f %f", Coordinates.x, Coordinates.y); //after input, throws error.
    printf("Please enter your center coordinate and your radius:");
    scanf("%f %f", insideCircle.radius, insideCircle.center.x, insideCircle.center.y);
    printf("%f %f %f %f %f", Coordinates.x, Coordinates.y, insideCircle.radius, insideCircle.center.x, insideCircle.center.y);

//More code for checking if distance > or < or = to radius to be added.
    getch();
}


对于scanf(),您需要将变量的地址作为参数传递给所提供的转换说明符,例如

1
2
scanf("%f %f", &(Coordinates.x), &(Coordinates.y));
               ^                 ^

以及用于其他用途。

也就是说,必须检查scanf()调用的返回值以确保成功。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*i think this piece of code will help you*/

#include <stdio.h>
#include <stdlib.h>

typedef struct {
        float x;
        float y;
}Point;

typedef struct {
        Point center;
        float radius;
}Circle;

int main()
{
        Point Coordinates;
        Coordinates.x = 0;
        Coordinates.y = 0;
        Circle insideCircle;
        float distance;
        printf("Please enter the coordinates of your point:");
        scanf("%f %f", &(Coordinates.x), &(Coordinates.y) ); //scanf requires &
        printf("Please enter your center coordinate and your radius:");
        scanf("%f %f %f", &(insideCircle.radius), &(insideCircle.center.x), &(insideCircle.center.y) );//scanf requires &
        printf("%f %f %f %f %f", Coordinates.x, Coordinates.y, insideCircle.radius, insideCircle.center.x, insideCircle.center.y);

        //More code for checking if distance > or < or = to radius to be added.
}