The position of a point

Question:

Specify, design, and implement a class that can be used to keep track of the position of a point in three-dimensional space. For example, consider the point drawn at the picture below. The point shown there has three coordinates: x = 2.5, y = 0, and z = 2.0. Include member functions to set a point to a specified location, to shift a point a given amount along one of the axes, and to retrieve the coordinates of a point. Also provide member functions that will rotate the point by a specified angle around a specified axis.
figure
To compute these rotations, you will need a bit of trigonometry. Suppose you have a point with coordinates x, y, and z. After rotating this point (counter-clockwise) by an angle θ \theta θ, the point will have new coordinates, which we’ll call x ′ x' x, y ′ y' y, and z ′ z' z. The equations for the new coordinates use the cmath library functions sin and cos, as shown here:
After a θ \theta θ rotation around the x-axis: x ′ = x x' = x x=x y ′ = y cos ⁡ ( θ ) − z sin ⁡ ( θ ) y'=y\cos(\theta)-z\sin(\theta) y=ycos(θ)zsin(θ) z ′ = y sin ⁡ ( θ ) + z cos ⁡ ( θ ) z'=y\sin(\theta)+z\cos(\theta) z=ysin(θ)+zcos(θ)
After a θ \theta θ rotation around the y-axis: x ′ = x cos ⁡ ( θ ) + z sin ⁡ ( θ ) x'=x\cos(\theta)+z\sin(\theta) x=xcos(θ)+zsin(θ) y ′ = y y'=y y=y z ′ = − x sin ⁡ ( θ ) + z cos ⁡ ( θ ) z'=-x\sin(\theta)+z\cos(\theta) z=xsin(θ)+zcos(θ)
After a θ \theta θ rotation around the z-axis: x ′ = x cos ⁡ ( θ ) − y sin ⁡ ( θ ) x'=x\cos(\theta)-y\sin(\theta) x=xcos(θ)ysin(θ) y ′ = x sin ⁡ ( θ ) + y cos ⁡ ( θ ) y'=x\sin(\theta)+y\cos(\theta) y=xsin(θ)+ycos(θ) z ′ = z z'=z z=z

My answer:

point.h

#pragma once
class point {
public:
	point() {
		x = y = z = 0;
	};
	point(double m, double n, double q) {
		set_position(m, n, q);
	};
	void set_position(double m, double n, double q);
	void move_x(double m);
	void move_y(double m);
	void move_z(double m);
	void rotate_x(double a);
	void rotate_y(double a);
	void rotate_z(double a);
	void print_point();
private:
	double x, y, z;
};

point.cpp

#include "pch.h"
#include "point.h"
#include <cmath>
#include <iostream>

void point::set_position(double m, double n, double q)
{
	x = m;
	y = n;
	z = q;
}

void point::move_x(double m)
{
	std::cout << "shift along x-axis by " << m << std::endl;
	x += m;
}

void point::move_y(double m)
{
	std::cout << "shift along y-axis by " << m << std::endl;
	y += m;
}

void point::move_z(double m)
{
	std::cout << "shift along z-axis by " << m << std::endl;
	z += m;
}

void point::rotate_x(double a)
{
	std::cout << "rotate around the x-axis by " << a << std::endl;
	double tmp_y;
	tmp_y = y * cos(a) - z * sin(a);
	z = y * sin(a) + z * cos(a);
	y = tmp_y;
}

void point::rotate_y(double a)
{
	std::cout << "rotate around the y-axis by " << a << std::endl;
	double tmp_x;
	tmp_x = x * cos(a) + z * sin(a);
	z = -x * sin(a) + z * cos(a);
	x = tmp_x;
}

void point::rotate_z(double a)
{
	std::cout << "rotate around the z-axis by " << a << std::endl;
	double tmp_x;
	tmp_x = x * cos(a) - y * sin(a);
	y = x * sin(a) + y * cos(a);
	x = tmp_x;
}

void point::print_point()
{
	std::cout << "(x, y, z) = (" << x << ", " << y << ", " << z << ")" << std::endl;
}

测试代码:

#include "point.h"
#include <iostream>

#define PI 3.14159265

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();
}

结果:
结果

reference:

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
代码We now want to always redraw all the points that have ever been drawn in the panel, not just the last point. To do this, we must save the coordinates of all these points so that we can redraw them all one by one in the paintComponent method every time this method is called. To save the coordinates of the various mouse positions we click, replace the x and y instance variables of the MyPanel class with a single private instance variable called points of type ArrayList<Point>. The Point class is provided to you by Swing. In the constructor of MyPanel, initialize the points instance variable with a new arraylist object of the same type. In the mouseClicked method of the mouse listener, use the getPoint method of the mouse event object to get a Point object representing the position of the mouse click (that Point object internally stores both the x and y coordinates of the mouse click event). Then add this Point object to the arraylist using the arraylist’s add method. Then, in the paintComponent method, add a loop to draw in the panel all the points of the arraylist. You can get the number of elements in the arraylist by using the size method of the arraylist; you can access a specific element of the arraylist at index i by using the get(i) method of the arraylist (element indexes start at zero in an arraylist). The Point class has getX and getY methods to get the coordinates of the point (these two methods return values of type double so you need to cast the returned values into the int type before you can use them to draw a point).
最新发布
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Memories off

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

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

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

打赏作者

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

抵扣说明:

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

余额充值