机器人技术第三次作业:用面向对象的思维设计相关类,从而实现直线与直线、直线与圆、直线与矩形的交点。

本文探讨如何运用面向对象编程思想设计类,以计算直线与直线、直线与圆以及直线与矩形的交点。通过数学公式直接求解直线之间的交点,同时解析处理直线与矩形交点问题。
摘要由CSDN通过智能技术生成

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

基本思想:
直线与直线,直线与圆:利用公式直接求解
直线与矩形:分解成直线与直线的交点:



#include <iostream>

#include <math.h>

class VecPosition//坐标

{
   

public:

    double mx;

    double my;

public:

 

    void setpoint(double x,double y)

    {
   

        mx = x;

        my = y;

    }

};

class Line直线

{
   

public:

    double a;

    double b;//ax+by+c=0

    double c;

public:

    void setLine(double ma,double mb,double mc)

    {
   

        a=ma;

        b=mb;

        c=mc;

    }

 

};

class Circle

{
   

public:

    VecPosition 
center;

    double R;

public :

 

    void setcircle(VecPosition my_center,
double my_R)

    {
   

        center = my_center;

        R = my_R;

    }

};

class Rectangel矩形

{
   

public:

    VecPosition top;

    VecPosition bottem;

public:

    void setRectangel(VecPosition
mytop,VecPosition mybottom)

    {
   

        top=mytop;

        bottem=mybottom;

    }

 

};

using namespace std;

直线与直线的交点//

VecPosition
LinecrossLine(Line l1,Line l2)

{
   

    VecPosition cross;

    double k1=(-1)*l1.a/l1.b;

    double k2=(-1)*l2.a/l2.b;

 

    double d;

    if(k1==k2)

    {
   

        cout<<"没有交点"<<endl;

        return cross;

    }

    else

    {
   

        d=l1.a*l2.b - l2.a*l1.b;

        cross.mx = (l1.b*l2.c-l2.b*l1.c)/d;

        cross.my = (l1.c*l2.a-l2.c*l1.a)/d;

        return cross;

    }

 

}

直线与圆的交点//

void LinecrossCircle(Line
l,Circle c)

{
   

 

    double k;

    double b;

    double dc;//直线与与圆心的距离

    double x1,y1,x2,y2;

 

    
为了实现线与线、线与以及线与矩形交点计算,我们可以创建三个相关直线矩形,并提供一些公共接口来进行相互之间的判断。以下是C++的一个简单示例: ```cpp #include <iostream> #include <cmath> // 直线 class Line { public: // 构造函数,传入斜率和y截距 Line(double m, double c) : slope(m), intercept(c) {} // 检查两条直线是否相交 bool intersects(const Line& other) const { // 简单地检查斜率是否相等,若相等则说明平行,无交点 return slope != other.slope; } private: double slope; // 斜率 double intercept; // y截距 }; // class Circle { public: // 构造函数,传入心和半径 Circle(double x, double y, double r) : center{x, y}, radius{r} {} // 检查直线是否有交点 bool intersects(const Line& line) const { double d = std::sqrt(std::pow(line.intercept - center.y, 2) + std::pow(line.slope * center.x + line.intercept - center.y, 2)); return d <= radius; } private: std::pair<double, double> center; // 心坐标 double radius; // 半径 }; // 矩形 class Rectangle { public: // 构造函数,传入左上角和右下角坐标 Rectangle(double x1, double y1, double x2, double y2) : top_left{x1, y1}, bottom_right{x2, y2} {} // 检查直线是否与矩形交点,这里简化为只考虑垂直或水平线的情况 bool intersects(const Line& line) const { if (std::abs(line.slope) == 0) { // 垂直线,比较x坐标 return line.intercept >= top_left.y && line.intercept <= bottom_right.y; } else { // 水平线,比较y坐标 return line.intercept + line.slope * top_left.x >= top_left.y && line.intercept + line.slope * bottom_right.x <= bottom_right.y; } } private: std::pair<double, double> top_left, bottom_right; // 矩形顶点坐标 }; // 示例使用 int main() { Line line1(1, 0); // 直线 y = x Line line2(2, 0); // 另一条直线 y = 2x Circle circle(0, 0, 5); // 心(0,0),半径5 Rectangle rectangle(0, 0, 5, 5); // 矩形 [0,5]x[0,5] std::cout << "Lines intersect: " << (line1.intersects(line2) ? "Yes" : "No") << '\n'; std::cout << "Circle intersects line: " << (circle.intersects(line1) ? "Yes" : "No") << '\n'; std::cout << "Rectangle intersects line: " << (rectangle.intersects(line1) ? "Yes" : "No") << '\n'; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值