Stroustrup's Superellipse

HI,

I'm working through Stroustrup's PPP, on the graphics chapter 12, exercise 12 I need to draw a superellipse. Using, the maths function from Wikipedia I can almost get this working. When the n value is 2, it draws a circle, which it should. But, when I have an n value of 4, instead of drawing a square with rounded corners I get a square with sharp corners. Can anyone spot any problems with what I'm doing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<FL/Fl.H>
#include<FL/Fl_Box.H>
#include<FL/Fl_Window.H>

#include "Graph_2.h"
#include "Simple_window_2.h"
#include"std_lib_facilities.h"

double sgn(double d)
{
	if (d>0) return 1;
	else if (d<0) return -1;
	else return 0;
}

int main()
{
	using namespace Graph_lib;
	Point tl(100, 100);
	Simple_window win(tl, 1200, 600, "Superellipse");

	const double pi = (22.0 / 7.0);
	int a = 100;
	int b = 100;
	double n = 4;
	
	Open_polyline Super_E;
	
	for (double angle = 0.0; angle < 2 * pi; angle += 2 * pi / 50)
	{
		int na = 2 / n;
		double x = pow(abs(cos(angle)), na)*a*sgn(cos(angle));
		double y = pow(abs(sin(angle)), na)*b*sgn(sin(angle));
		Super_E.add(Point(x+200, y+200));
	}
	Super_E.set_style(Line_style(Line_style::solid, 2));
	win.attach(Super_E);
	win.wait_for_button();
}
Make na a double on line 31. Otherwise it will round down to zero.
Marvelous! Thank you so much.
Topic archived. No new replies allowed.