A line segment

Question:

In three-dimensional space, a line segment is defined by its two endpoints. Specify, design, and implement a class for a line segment. The class should have two private member variables that are points from the previous project.

My answer:

在point类声明中添加

double point::get_x() const
{
	return x;
}

double point::get_y() const
{
	return y;
}

double point::get_z() const
{
	return z;
}

line类声明

class line {
public:
	line(point m, point n) {
		p1 = m;
		p2 = n;
	}
	double length();
private:
	point p1, p2;
};

line类实现

double line::length()
{
	return sqrt(pow(p1.get_x() - p2.get_x(), 2) + pow(p1.get_y() - p2.get_y(), 2) + pow(p1.get_z() - p2.get_z(), 2));
}

 测试:

int main()
{
	point a(2.5, 0, 2.0);
	a.move_x(1);
	a.move_y(1.5);
	a.move_z(-2.5);
	a.rotate_x(PI / 2);
	a.rotate_y(PI / 4);
	a.rotate_z(3 * PI / 4);
	a.print_point();
	a.set_position(5, 5, 5);
	a.print_point();
	point b(3, 4, 5);
	line l(a, b);
	std::cout << "The length of l is " << l.length() << std::endl;
}

 结果:

Reference:

整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p121

public override bool GroupRun(ref string message, ref CogToolResultConstants result) { // To let the execution stop in this script when a debugger is attached, uncomment the following lines. // #if DEBUG // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); // #endif // Run each tool using the RunTool function foreach(ICogTool tool in Tools) RunTool(tool, ref message, ref result); // 输入距离 double a = this.Inputs.R/2; Point[] L1 = FindParallelLine(new Point(this.Inputs.LineSegment1.StartX,this.Inputs.LineSegment1.StartY), new Point(this.Inputs.LineSegment1.EndX,this.Inputs.LineSegment1.EndY), a); Point[] L2 = FindParallelLine(new Point(this.Inputs.LineSegment2.StartX,this.Inputs.LineSegment2.StartY), new Point(this.Inputs.LineSegment2.EndX,this.Inputs.LineSegment2.EndY), a); Point PX1 = GetIntersectionPoint(L1[0].X, L1[0].Y, L1[1].X, L1[1].Y, L2[0].X, L2[0].Y, L2[1].X, L2[1].Y); this.Outputs.X1 = PX1.X; this.Outputs.Y1 = PX1.Y; Point[] L3 = FindParallelLine(new Point(this.Inputs.LineSegment3.StartX, this.Inputs.LineSegment3.StartY), new Point(this.Inputs.LineSegment3.EndX, this.Inputs.LineSegment3.EndY), a); Point[] L4 = FindParallelLine(new Point(this.Inputs.LineSegment4.StartX, this.Inputs.LineSegment4.StartY), new Point(this.Inputs.LineSegment4.EndX, this.Inputs.LineSegment4.EndY), a); Point PX2 = GetIntersectionPoint(L3[0].X, L3[0].Y, L3[1].X, L3[1].Y, L4[0].X, L4[0].Y, L4[1].X, L4[1].Y); this.Outputs.X2 = PX2.X; this.Outputs.Y2 = PX2.Y; //输出值变量值除以1000 this.Outputs.D1 = (PX1.Y+a)/1000; this.Outputs.D2 = (PX2.Y+a)/1000; this.Outputs.DD = Math.Abs( this.Outputs.D1-this.Outputs.D2); return false; } //线段平移 public static Point[] FindParallelLine(Point start, Point end, double distance) { double length = Math.Sqrt(Math.Pow(end.X - start.X, 2) + Math.Pow(end.Y - start.Y, 2)); double dx = (end.X - start.X) / leng
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值