检查圆圈内部或外部的点(仅限结构实现)

时间:2016-03-16 11:54:11

标签: c struct scanf

首先,我已经完成了使用结构实现此任务的任务。

我需要检查一个点是否在圆圈内外。

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

输出:圆圈内/外的点。

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

我知道逻辑,但是我的struct语法失败了。你能帮助我吗?

#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();
}

2 个答案:

答案 0 :(得分:1)

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

android.R.id.text1

以及其他用途。

尽管如此,您必须检查scanf()来电的返回值,以确保成功。

答案 1 :(得分:0)

/*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.
}