求两条直线的交点!! C++

要求:用面向对象的方法继承的方法实现。⑴系统总体方案⑵设计思路和主要步骤⑶各功能模块和流程图⑷设计代码完成这些问题..谢谢..我分只有5分..... 要求:用面向对象的方法继承的方法实现。

⑴ 系统总体方案
⑵ 设计思路和主要步骤
⑶ 各功能模块和流程图
⑷ 设计代码

完成这些问题..谢谢..
我分只有5分..
展开
 我来答
zyq_07_28
推荐于2016-12-01 · TA获得超过258个赞
知道小有建树答主
回答量:185
采纳率:0%
帮助的人:0
展开全部
代码和注释给你, 自己整理吧

//.h文件
#ifndef _LINE_H_
#define _LINE_H_

#include <stdio.h>
#include <iostream.h>

class Point
{
public:
/* 点的坐标 */
int x1;
int y1;

public:
void SetXY(int x, int y);
void GetXY(int &x, int &y);

/* 2个构造函数 */
Point();
Point(int x, int y);
};

class Line : public Point /* line继承point */
{
public:
/* 另一个点的坐标 */
int x2;
int y2;

public:
void SetPoint(Point* point1, Point* point2);

/* 3个构造函数 */
Line();
Line(int x1, int y1, int x2, int y2);
Line(Point* point1, Point* point2);

/* 是否相交
** 相交返回0, 交点为intersect_point
** 不相交返回-1, intersect_point为空
** 两直线相同返回1, intersect_point为空
*/
int Intersect(Line* another_line, Point* intersect_point);
};

#endif

//.c文件
#include "1.h"

Point::Point()
{
x1 = 0;
y1 = 0;
}

Point::Point(int x, int y)
{
x1 = x;
y1 = y;
}

void Point::SetXY(int x, int y)
{
x1 = x;
y1 = y;
}

void Point::GetXY(int &x, int &y)
{
x = x1;
y = y1;
}

Line::Line()
{
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
}

Line::Line(int x1, int y1, int x2, int y2)
{
this->x1 = x1;
this->x2 = x2;
this->y1 = y1;
this->y2 = y2;
}

void Line::SetPoint(Point* point1, Point* point2)
{
x1 = point1->x1;
y1 = point1->y1;
x2 = point2->x1;
y2 = point2->y1;
}

Line::Line(Point* point1, Point* point2)
{
x1 = point1->x1;
y1 = point1->y1;
x2 = point2->x1;
y2 = point2->y1;
}

int Line::Intersect(Line* another_line, Point* intersect_point)
{
/* y = ax + b */
int a_my, b_my;
b_my = (x1 * y2 - y1 * x2) / (x1 - x2);
a_my = (y1 - y2) / (x1 - x2);

/* check if point */
Point* point = (Point*)another_line;
if(a_my * point->x1 + b_my == point->y1)
{
intersect_point = point;
return 0;
}

int a_other, b_other;
a_other = (another_line->x1 * another_line->y2 - another_line->y1 * another_line->x2) / (another_line->x1 - another_line->x2);
b_other = (another_line->y1 - another_line->y2) / (another_line->x1 - another_line->x2);

if(a_other == a_my)
{
if(b_my == b_other)
{
intersect_point = NULL;
return -1; //not intersect
}
else
{
return 1; //the same
}
}
else
{
intersect_point->x1 = ((b_other - b_my) / (a_my - a_other));
intersect_point->y1 = (a_my * intersect_point->x1 + b_my);
return 0; //intersect
}

}

/* test */
int main()
{
Point point1(0, 1);
Point point2(1, 2);

Line line1(0, 1, 1, 2);
Line line2(&point1, &point1);

Line po;
int a = line1.Intersect((Line*)&line2, &po);
if(a == 0)
{
cout<<"yes"<<endl;
cout<<"x = "<<po.x1<<", y = "<<po.y1<<endl;
}
else if(a == -1)
{
cout<<"no"<<endl;
}
else
{
cout<<"same"<<endl;
}

return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式