练习13,来自Stroustrup的编程原理和使用c ++实践的第12章。
超椭圆是由等式
定义的二维形状
在网络上查找 superellipse ,以便更好地了解这些内容 形状看起来像。编写一个程序,绘制“星形”模式 在超椭圆上连接点。取a,b,m,n和N为 参数。 在由a,b,m定义的超椭圆上选择N个点, 和n。为了某些定义,使点间距相等 “相等”将这N个点中的每一个连接到一个或多个其他点 (如果您愿意,可以将点数连接到另一个点 参数或只使用N-1,即所有我的点数。)
我有一个向量,其中包含可以构建超椭圆的点 我不能得到练习的第二部分 - 如何找到位于超椭圆上的N点来建造星星?
感谢
//------------------------------------------------------------------------------
struct Point {
int x, y;
Point(int xx, int yy) : x(xx), y(yy) { }
Point() :x(0), y(0) { }
};
//------------------------------------------------------------------------------
int sgn(double d) {
if (d < 0)
return -1;
if (d == 0)
return 0;
if (d > 0)
return 1;
// exception
error("sgn: something gone wrong\n");
}
//------------------------------------------------------------------------------
vector<Point> superellipse(double a, double b, double m, double n, double precision = 0.01, int xCenter = 200, int yCenter = 200) {
if (precision >= 1.00 || precision < 0.001)
error ("use numbers from range [0.001 ; 1.00) for precision parametr\n");
vector<Point> points;
Point temp;
Point P;
for (double d = -1.0; d < 1.00; d += precision) {
double t = d*M_PI;
int x = pow((abs(cos(t))),2.0/m) * a * sgn(cos(t));
int y = pow((abs(sin(t))),2.0/n) * b * sgn(sin(t));
P = Point(x + xCenter, y + yCenter);
if (P != temp) // ignore dublicates
points.push_back(P);
temp = P;
}
return points;
}
//------------------------------------------------------------------------------
答案 0 :(得分:2)
1.scratch
vector<Point> superellipse (/*...*/) {
/* */
return points;
}
2.put
class Superellipse {
std::vector<Point> _points;
void draw_connected(const Point& A, const Point& B);
public:
void draw_star(/* params */);
Superellipse(/* params*/){
/* initialize _points*/
}
};
我无法进行第二部分练习 - 如何找到N点 建立星星的超级圆形?
??? 你已经在向量中得到了分数。
http://www.stroustrup.com/Programming/programming_ch12.pdf
如果您愿意,可以将点数连接到另一个点 参数或只使用N-1,即所有其他点
void Superellipse::draw_star (/* */){
int N = _points.size();
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
draw_connected (_points[i], _points[j]);
}
}
}