#include <iostream>
#include<string>;
using namespace std;
class point
{
private:
double x;
double y;
public:
void cinX(double a) {
x = a;
}
double getX() {
double a = x;
return a;
}
void cinY(double b) {
y = b;
}
double getY() {
double a = y;
return a;
}
};
class circle {
private:
double rX;
double rY;
double r;
public:
void cinrX(double a) {
rX = a;
}
double getrX() {
double x = rX;
return x;
}
void cinrY(double a) {
rY = a;
}
double getrY() {
double a = rY;
return a;
}
void cinr(double a) {
r = a;
}
double getr() {
double a = r;
return a;
}
};
double getDistance(point p, circle c) {
double d = sqrt((p.getX() - c.getrX()) * (p.getX() - c.getrX()) + (p.getY() - c.getrY()) * (p.getY() - c.getrY()));
return d;
}
int main()
{
point p1;
p1.cinX(2);
p1.cinY(2);
circle c1;
c1.cinrX(4);
c1.cinrY(5);
c1.cinr(30);
double d = getDistance(p1, c1);
cout << "d = " << d << endl;
if (d > c1.getr())
{
cout << "点在圆外" << endl;
return 0;
}
else {
if (d = c1.getr()) {
cout << "点在圆上" << endl;
return 0;
}
else
cout << "点在圆内" << endl;
}
}
点在圆上判断有问题,应该是==
又称再哈西法,再原来哈西函数的基础上再准备一个哈西函数2,如地址冲突,运行函数2,如又冲突,则函数2得到的值加一,再加二直到不在冲突。
问题解答:
根据问题描述,“所有的“点在圆内”的情况都被算成了“点在圆上”,是什么原因造成的呢?请问该如何解决这个问题呢?”
这个问题属于计算机图形学中的基础问题,一般解决方法有两种:
方法一:使用数学公式判断点与圆的位置关系。
假设圆心坐标为(x0,y0),半径为r,则点P(xp,yp)与圆心的距离为d=sqrt((xp-x0)^2+(yp-y0)^2)。当d>r时,点P在圆的外面;当d=r时,点P在圆上;当d<r时,点P在圆的里面。
具体实现代码为:
import math
def judge_position(x0, y0, r, xp, yp):
d = math.sqrt((xp-x0)**2 + (yp-y0)**2)
if d > r:
return "point is outside of the circle"
elif d == r:
return "point is on the circle"
else:
return "point is inside of the circle"
方法二:使用OpenGL库中的函数判断点与圆的位置关系。
在计算机图形学中,通常使用OpenGL来进行图形绘制。OpenGL库中提供了判断点与圆的位置关系的函数gluDisk(),可以方便地判断一个点是否在圆内。具体实现方法为:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def judge_position(x0, y0, r, xp, yp):
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0, 1.0, 1.0)
gluDisk(gluNewQuadric(), x0, y0, r, 1, 1)
glFlush()
pixel = glReadPixels(xp, yp, 1, 1, GL_RGB, GL_FLOAT)
if pixel[0][0][0] == 1.0:
return "point is inside of the circle"
else:
return "point is outside of the circle"
以上两种方法都可以判断点与圆的位置关系,具体选择哪种方法,可以根据实际情况进行选择。如果只是在纯python环境下实现函数,建议使用方法一;如果是在OpenGL环境下实现图形绘制,建议使用方法二。
希望以上解答能够对您有所帮助。如有不懂之处,欢迎继续提问。