
please wait
Draw a series of regular polygons, one inside the other. The innermost should be an equilateral triangle, enclosed by a square, enclosed by a pentagon, etc. For the mathematically adept only: let all the points of each N -polygon touch sides of the (N+1) -polygon. Hint: The trigonomet- ric functions are found in <cmath> (§24.8, §B.9.2). |
|
|
|
|
|
|
|
|
The loop is good and all, but I think the exercise is asking for me to hard-code the values. |
So for each polygon, I add points with x-axis being win_width/2 and win_height/2 if I want it centered in the window? |
|
|
From the primitive graphics elements you see in this window, you can build dis- plays of just about any complexity and subtlety. For now, just note a peculiarity of the code in this chapter: there are no loops, no selection statements, and all data was “hardwired” in. The output was just composed of primitives in the simplest possible way. Once we start composing these primitives using data and algorithms, things will start to get interesting. |
But if I use those loops, do I use a separate one for each polygon I draw? |
How do I determine the number of points in the loop and make it stop where I intend it to? |
|
|
sides
variable constexpr
, I won't be able to change it, right? And you're saying I have to write all that more than once in the same program for each polygon I have to make?
But if I make the sides variable constexpr , I won't be able to change it, right? And you're saying I have to write all that more than once in the same program for each polygon I have to make? |
|
|
Point
requires the coordinates as int
s, so using a double
is flagging a "conversion from 'double' to 'int' requires a narrowing conversion" error on both the x- and y-coordinates. And trying to use a copy constructor has the same thing happen. How do I fix this?
|
|
1>c:\users\osman\programming\visual studio 2017\projects\programming_principles_practice_using_c++\chapter12ex11\chapter12ex11\chapter12ex11.cpp(46): error C2398: Element '1': conversion from 'double' to 'int' requires a narrowing conversion 1>c:\users\osman\programming\visual studio 2017\projects\programming_principles_practice_using_c++\chapter12ex11\chapter12ex11\chapter12ex11.cpp(46): error C2398: Element '2': conversion from 'double' to 'int' requires a narrowing conversion |
Vector_ref
has no begin()
or end()
function, so a range-based for loop won't work for it.
|
|
static_cast<int>
and it worked. But now the polygons aren't showing up in the window. What happened? Did I use the wrong value for the radius
variable or is there some other mistake?runtime_error
exception got triggered because "two polygons lie in a straight line". Now what do I do?
|
|
for (int i = 0; i < polys.size(); ++i)
I guess this vector is empty since you don't add anything to it.Point X=304, Y=-200 Point X=297, Y=-196 Point X=298, Y=-203 Point X=304, Y=-200 Point X=299, Y=-196 Point X=296, Y=-200 Point X=300, Y=-204 Point X=304, Y=-200 Point X=301, Y=-196 Point X=296, Y=-197 Point X=296, Y=-202 Point X=301, Y=-203 Point X=304, Y=-200 Point X=301, Y=-196 Point X=297, Y=-196 Point X=296, Y=-200 Point X=298, Y=-203 Point X=302, Y=-203 Point X=304, Y=-200 Point X=302, Y=-196 Point X=299, Y=-196 Point X=296, Y=-198 Point X=296, Y=-201 Point X=298, Y=-203 Point X=302, Y=-203 Point X=303, Y=-200 Point X=304, Y=-200 Point X=302, Y=-197 Point X=299, Y=-196 Point X=297, Y=-197 Point X=296, Y=-200 Point X=297, Y=-202 Point X=300, Y=-204 Point X=302, Y=-202 Point X=304, Y=-200 Point X=303, Y=-197 Point X=300, Y=-196 Point X=297, Y=-196 Point X=296, Y=-198 Point X=296, Y=-201 Point X=298, Y=-203 Point X=300, Y=-203 Point X=303, Y=-202 Point X=304, Y=-200 Point X=303, Y=-197 Point X=301, Y=-196 Point X=298, Y=-196 Point X=296, Y=-197 Point X=296, Y=-200 Point X=296, Y=-202 Point X=298, Y=-203 Point X=301, Y=-203 Point X=303, Y=-202 ** Size of polygons = 8 |
|
|
delete
, but I guess that's not it either.new
and delete
, which took care of that problem. But now there's that same runtime error again.
|
|
poly
every iteration of the outer for-loop. Thus, you will end up adding points for a triangle, square, pentagon, etc... to the same polygon. With a polygon, the lines can not intersect, which is probably why you're getting a runtime error.
|
|
|
|
delete[] poly;
Vector_ref
do it on its own and just fix my placement of polygon initialization?abs()
now to make sure that both values are positive. But I still get nothing showing up on the window.
|
|
abs
from the x-axis value.
|
|
|
|
|
|