### chapter 13 Stroustrup text box, math for rounded box class

I'm trying to create a box with rounded corners using FLTK. I got the the box to print with the arcs in the right positions, but my math appears to be all wonky. When create a box with a width of 300, and height 200, the arcs float in space, instead of connecting to my line segments.

I'm taking 10% of the longer side, and subtracting it from each corner of the rectangle. e.g. if w>h then x axis for top left corner x = xy.x + (xy.x* 0.10) If the ratio of the h or w is too long, I can't even see the shorter sides.

Plus I thought that if I can take 10% from each corner, and I can make the arc foci 10% , and this would make up for that which was subtracted to create the shortened line segments.

Any advice or tips on the math?

namespace Graph_lib
{
//---------------------------------------------------------------------------------
struct Box : Graph_lib::Shape {

Box(Point xxyy, int ww, int hh);

void Top_segment ();
void Bottom_segment ();
void Left_side_segment ();
void Right_side_segment();
void Get_segments();

void draw_lines() const;

int height() const { return h; }
int width () const { return w; }

private:

Point xy; //left most corner

int h; // height
int w; // width

vector<Point> top_seg;
vector<Point> bottom_seg;
vector<Point> left_seg;
vector<Point> right_seg;

double longer_side_tenth; //10% of the width that will calculate the length to remove from each side to make room for the arcs
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Box::Box(Point xxyy, int ww, int hh): w(ww), h(hh), xy(xxyy)
{

if(w>h)
longer_side_tenth = (xy.x + w) * 0.10;
if(h>w)
longer_side_tenth = (xy.x + h) * 0.10;

if (h<=0 || w<=0) error("Bad rectangle: non-positive side");

Get_segments();

}
//------------------------------------------------------------------------------
void Box::Get_segments()
{
Top_segment ();
Bottom_segment ();
Left_side_segment ();
Right_side_segment();
}
//------------------------------------------------------------------------------
void Box::draw_lines() const
{

//top segment
fl_line(
top_seg[0].x,
top_seg[0].y,
top_seg[1].x,
top_seg[1].y
);

//bottom segment
fl_line(
bottom_seg[0].x,
bottom_seg[0].y,
bottom_seg[1].x,
bottom_seg[1].y
);

//left segment
fl_line(
left_seg[0].x,
left_seg[0].y,
left_seg[1].x,
left_seg[1].y
);

//right segmnet
fl_line(
right_seg[0].x,
right_seg[0].y,
right_seg[1].x,
right_seg[1].y
);

//top left arc
fl_arc(
xy.x,
xy.y,
longer_side_tenth,
longer_side_tenth,
90,
180);

//top right arc
fl_arc(
(xy.x + w)-longer_side_tenth,
xy.y,
longer_side_tenth,
longer_side_tenth,
0,
90);

//bottom left arc
fl_arc(
xy.x,
(xy.y + h) - longer_side_tenth,
longer_side_tenth,
longer_side_tenth,
180,
270);

//bottom right arc
fl_arc(
(xy.x + w) - longer_side_tenth,
(xy.y + h) - longer_side_tenth,
longer_side_tenth,
longer_side_tenth,
270,
360);

}
//------------------------------------------------------------------------------
void Box::Top_segment()
{

double top_seg_begin_w; //where the line segment will begin after deducting 10% of w;
double top_seg_end_w; //where the line segment will end after deducting 10% of w;

top_seg_begin_w = xy.x + longer_side_tenth;
top_seg_end_w = (xy.x + w) - longer_side_tenth;

double top_seg_y = xy.y;

top_seg.push_back(Point(top_seg_begin_w,top_seg_y));
top_seg.push_back(Point(top_seg_end_w,top_seg_y));

}

//------------------------------------------------------------------------------
void Box::Bottom_segment()
{
double bottom_seg_begin_w;
double bottom_seg_end_w;

bottom_seg_begin_w = xy.x + longer_side_tenth;
bottom_seg_end_w = (xy.x + w) - longer_side_tenth;

double y_bottom = xy.y + h;

bottom_seg.push_back(Point(bottom_seg_begin_w,y_bottom));
bottom_seg.push_back(Point(bottom_seg_end_w,y_bottom));
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void Box::Left_side_segment()
{
double left_seg_begin_h;
double left_seg_end_h;

left_seg_begin_h = xy.y + longer_side_tenth;

left_seg_end_h = (xy.y + h) - longer_side_tenth;

double x_left = xy.x;

left_seg.push_back(Point(x_left, left_seg_begin_h));
left_seg.push_back(Point(x_left, left_seg_end_h));

}
//------------------------------------------------------------------------------
void Box::Right_side_segment()
{
double right_seg_begin_h;
double right_seg_end_h;

right_seg_begin_h = xy.y + longer_side_tenth;
right_seg_end_h = (xy.y + h) - longer_side_tenth;

double x_right = xy.x + w;

right_seg.push_back(Point(x_right, right_seg_begin_h));
right_seg.push_back(Point(x_right, right_seg_end_h));
}
//---------------------------------------------------------------------------------
}//end Graph_lib
Topic archived. No new replies allowed.