题目:用面向对象的思维设计相关类,从而实现直线与直线、直线与圆、直线与矩形的交点。

要求各给出每个案例的至少一个示例的程序。
具体实现:
语言:C++        工具:Visual Studio2019。

 上图只讨论一般情况下的交点情况,特殊直线(平行X轴或Y轴)以及无交点情况不多赘述。

源代码:(仅供参考,水平有限,有错请指出)

#include<iostream>
using namespace std;
class Circle
{
public:
	float r;
	float x0;
	float y0;
	Circle(float x0, float y0, float r) {
		this->x0 = x0;
		this->y0 = y0;
		this->r = r;
	}
};
//矩形
class Rectangle
{
public:
	float TopRightY;
	float TopRightx;
	float BottomLeftY;
	float BottomLeftX;
	Rectangle(float TRy, float TRx, float BLy, float BLx) {
		this->TopRightx = TRx;
		this->TopRightY = TRy;
		this->BottomLeftX = BLy;
		this->BottomLeftY = BLx;
	}
};
//直线
class Line
{
public:
	float a;
	float b;
	float c;
	Line(float a, float b, float c) {
		this->a = a;
		this->b = b;
		this->c = c;
	}
	//求交点
	void intersect(Line otherline);//与直线
	void intersect(Circle cirle);//与圆
	void intersect(Rectangle rectangle);//与矩形
};
void Line::intersect(Line otherline)
{
	if (this->b * otherline.a - this->a * otherline.b == 0)//斜率相同
	{
		if (this->c == otherline.c)
		{
			cout << "两直线重合,有无数交点" << endl;
			return;
		}
		else
		{
			cout << "两直线平行无交点" << endl;
			return;
		}
	}
	else//求具体交点
	{
		float intersectX = (this->c * otherline.b - this->b * otherline.c) /
			(this->b * otherline.a - this->a * otherline.b);
		float intersectY = (this->a * otherline.c - this->c * otherline.a) /
			(this->b * otherline.a - this->a * otherline.b);
		cout << "两直线交点为:" << fixed << "(" <<intersectX << "," << intersectY << ")" << endl;
	}
}
void Line::intersect(Circle cirle)
{
	float distanceR;//直线到圆心的距离
	distanceR = abs(this->a * cirle.x0 + this->b * cirle.y0 + this->c) /(sqrt(this->a * this->a + this->b * this->b));
	cout << "直线与圆心距离:" << distanceR << endl;
	if (distanceR > cirle.r)
		cout << "该直线与圆无交点" << endl;
	else if (this->a == 0)//直线平行于X轴 
	{
		float x1, x2,y1, y2;
		x1 = cirle.x0 + sqrt(cirle.r * cirle.r - distanceR * distanceR);
		x2 = cirle.x0 - sqrt(cirle.r * cirle.r - distanceR * distanceR);
		y1 = y2 = -this->c / this->b;
		if (x1 == x2)
			cout << "直线与圆有一交点:(" << x1 << "," << y1 << ")" << endl;
		else
			cout << "直线与圆有两交点:(" << x1 << "," << y1 << ")" << ",(" << x2 << ", " << y2 << ")" << endl;
	}
	else if (this->b == 0)//直线平行于Y轴
	{
		float x1, x2,y1, y2;
		y1 = cirle.y0 + sqrt(cirle.r * cirle.r - distanceR * distanceR);
		y2 = cirle.y0 - sqrt(cirle.r * cirle.r - distanceR * distanceR);
		x1 = x2 = -this->c / this->a;
		if (y1 == y2)
			cout << "直线与圆有一交点:(" << x1 << "," << y1 << ")" << endl;
		else
			cout << "直线与圆有两交点:(" << x1 << "," << y1 << ")" << ",(" << x2 << ", " << y2 << ")" << endl;
	}
	else//一般直线
	{
		float x1, x2,y1, y2;
		float k = -this->a / this->b;
		float b = -this->c / this->b;
		float c = -cirle.x0;
		float d = -cirle.y0;
		x1 = -(sqrt((k * k + 1) * cirle.r * cirle.r - c * c * k * k + (2 * c * d + 2 * b * c) * k - d * d - 2 * b * d - b * b) + (b + d) * k + c)
			/ (k * k + 1);
		x2 = (sqrt((k * k + 1) * cirle.r * cirle.r - c * c * k * k + (2 * c * d + 2 * b * c) * k - d * d - 2 * b * d - b * b) - (b + d) * k - c)
			/ (k * k + 1);
		y1 = k * x1 + b;
		y2 = k * x2 + b;
		if (x1 == x2)
			cout << "直线与圆有一交点:(" << x1 << "," << y1 << ")" << endl;
		else
			cout << "直线与圆有两交点:(" << x1 << "," << y1 << ")" << ",(" << x2 << ", " << y2 << ")" << endl;
	}
}
void Line::intersect(Rectangle rectangle)
{
	if (this->a == 0)//直线平行于X轴
	{
		float Y = -this->c / this->b;
		if (Y == rectangle.BottomLeftY || Y == rectangle.TopRightY)
		{
			cout << "直线与矩形一边重合,有无数交点" << endl;
			return;
		}
		else if (Y < rectangle.BottomLeftY || Y > rectangle.TopRightY)
		{
			cout << "直线与矩形无交点<<endl";
			return;
		}
		else
		{
			cout << "直线与矩形两交点:(" << rectangle.BottomLeftX << "," << Y << "),(" << rectangle.TopRightx << "," << Y << ")" << endl;
			return;
		}
	}
	else if (this->b == 0)//直线平行于Y轴
	{
		float X = -this->c / this->a;
		if (X == rectangle.BottomLeftX || X == rectangle.TopRightx)
		{
			cout << "直线与矩形一边重合,有无数交点" << endl;
			return;
		}
		else if (X < rectangle.BottomLeftX || X > rectangle.TopRightx)
		{
			cout << "直线与矩形无交点<<endl";
			return;
		}
		else
		{
			cout << "直线与矩形两交点:(" << X << "," << rectangle.BottomLeftY << "),(" << X << "," << rectangle.TopRightY << ")" << endl;
			return;
		}
	}
	else//一般情况
	{
		float X1, X2, X3, X4, Y1, Y2, Y3, Y4;
		bool flag = false;//是否存在
		X1 = rectangle.BottomLeftX;
		Y1 = -(this->a * X1 + this->c) / this->b;
		X2 = rectangle.TopRightx;
		Y2 = -(this->a * X2 + this->c) / this->b;
		Y3 = rectangle.BottomLeftY;
		X3 = -(this->b * Y3 + this->c) / this->a;
		Y4 = rectangle.TopRightY;
		X4 = -(this->b * Y4 + this->c) / this->a;
		if (Y1 <= Y4 && Y1 >= Y3)
		{
			if (Y1 == -0)
				Y1 = 0;
			cout << "存在交点:(" << X1 << "," << Y1 << ")";
			flag = true;
		}
		if (Y2 <= Y4 && Y2 >= Y3)
		{
			if (Y2 == -0)
				Y2 = 0;
			cout << "存在交点:(" << X2 << "," << Y2 << ")";
			flag = true;
		}
		if (X3 < X2 && X3 > X1)
		{
			if (X3 == -0)
				X3 = 0;
			cout << "存在交点:(" << X3 << "," << Y3 << ")";
			flag = true;
		}if (X4 < X2 && X4 > X1)
		{
			if (X4 == -0)
				X4 = 0;
			cout << "存在交点:(" << X4 << "," << Y4 << ")";
			flag = true;
		}
		if (!flag)
			cout << "无交点" << endl;
		cout << endl;
	}
}
int main() 
{
	Line line(1, 1, -1);
	Line otherline(-1, 1, 0);
	Rectangle rectangle(2, 2, 1, -1);
	Circle circle(0.0, 0.0, 1);
	cout << "直线1:" << line.a << "x+" << line.b << "y+" << line.c << "=0;  "
		<< "直线2:" << otherline.a << "x+" << otherline.b << "y+" << otherline.c << "=0;  "
		<< "圆:x0=" << circle.x0 << ",y0=" << circle.y0 << ",r=" << circle.r
		<< ";  矩形:左下=(" << rectangle.BottomLeftX << "," << rectangle.BottomLeftY << "),右上=(" << rectangle.TopRightx << "," << rectangle.TopRightY << ")" << endl;
	cout << "******************************************************" << endl;
	cout << "直线与直线" << endl;
	line.intersect(otherline);
	cout << "******************************************************" << endl;
	cout << "直线与矩形" << endl;
	line .intersect(rectangle);
	cout << "******************************************************" << endl;
	cout << "直线与圆" << endl;
	line.intersect(circle);
}

实现效果:

其他作业: 

合肥工业大学2021机器人技术作业三icon-default.png?t=N7T8https://blog.csdn.net/qq_52791068/article/details/122642744?spm=1001.2014.3001.5502

合肥工业大学2021机器人技术作业四icon-default.png?t=N7T8https://blog.csdn.net/qq_52791068/article/details/122643073?spm=1001.2014.3001.5502

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐