c++学习笔记——二维坐标下点和圆的关系

代码实现了计算点到圆心的距离,if判断出了点与圆的位置关系,同时利用EasyX图形绘制库画出了点和圆的图像,使得观察更加直观

#include <iostream>
#include <cmath>
#include <graphics.h>
#include <conio.h>
using namespace std;


class Circle_Point
{
public:
    Circle_Point() // 构造函数,初始化指针成员
    {
        c_xy = new float[2];
        p_xy = new float[2];
    }

    float distance()
    {
        return sqrt(pow((c_xy[0] - p_xy[0]), 2) + pow((c_xy[1] - p_xy[1]), 2));
    }

    //给圆心坐标赋值
    void setCircle_Center(float* xy)
    {
        c_xy[0] = xy[0];
        c_xy[1] = xy[1];
    }
    //获得圆心坐标
    float* getCircle_Center()
    {
        return c_xy;
    }
    //给点坐标赋值
    void setPoint_Center(float* xy)
    {
        p_xy[0] = xy[0];
        p_xy[1] = xy[1];
    }

    //获得点坐标
    float* getPoint_Center()
    {
        return p_xy;
    }

    //设置圆半径
    void setRadius(float r)
    {
        m_r = r;
    }
    //获取圆半径
    float getRadius()
    {
        return m_r;
    }
private:
    float m_r;
    float* c_xy;
    float* p_xy;
};

Circle_Point cl;

void Circle_Relationship_Point()
{
    float dis = cl.distance();
    if (dis > cl.getRadius())
    {
        cout << "点在圆外" << endl;
    }
    else if(dis < cl.getRadius())
    {
        cout << "点在圆内" << endl;
    }
    else if (dis == cl.getRadius())
    {
        cout << "点在圆上" << endl;
    }
}

// 使用 Bresenham 画圆法
void Circle_Bresenham(float x, float y, int color)
{
    float r = cl.getRadius();
    float tx = 0, ty = cl.getRadius(), d = 3 - 2 * r;

    while (tx <= ty)
    {
        // 利用圆的八分对称性画点
        putpixel(x + tx, y + ty, color);
        putpixel(x + tx, y - ty, color);
        putpixel(x - tx, y + ty, color);
        putpixel(x - tx, y - ty, color);
        putpixel(x + ty, y + tx, color);
        putpixel(x + ty, y - tx, color);
        putpixel(x - ty, y + tx, color);
        putpixel(x - ty, y - tx, color);

        if (d < 0)        // 取上面的点
            d += 4 * tx + 6;
        else            // 取下面的点
            d += 4 * (tx - ty) + 10, ty--;

        tx++;
    }
}

void putpoint(int x, int y, int size, COLORREF color) {
    int halfSize = size / 2;
    for (int i = x - halfSize; i <= x + halfSize; i++) {
        for (int j = y - halfSize; j <= y + halfSize; j++) {
            putpixel(i, j, color);
        }
    }
}

int main()
{
    cl.setRadius(20);

    float xy_1[] = { 320,240 };
    cl.setCircle_Center(xy_1);
    cl.getCircle_Center();

    float xy_2[] = { 100,240 };
    cl.setPoint_Center(xy_2);
    cl.getPoint_Center();
    cout << "圆心到点的距离为:"<< cl.distance() << endl;
    Circle_Relationship_Point();


    float Bre_xy1[2];
    float Bre_xy2[2];

    float* test;
    float* test2;

    test = cl.getCircle_Center();
    test2 = cl.getPoint_Center();

    Bre_xy1[0] = test[0];
    Bre_xy1[1] = test[1];

    Bre_xy2[0] = test2[0];
    Bre_xy2[1] = test2[1];


    initgraph(640, 480);
    Circle_Bresenham(Bre_xy1[0], Bre_xy1[1], WHITE);

    //putpixel(Bre_xy2[0], Bre_xy2[0], RED);
    putpoint(Bre_xy2[0], Bre_xy2[1], 5, RED);
    setorigin(320, 240);

    //cout << Bre_xy2[0] << endl;
    //cout << Bre_xy2[1] << endl;
    
    _getch();
    closegraph();
    system("pause");
    return 0;
}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值