❔ Circle

// In Main (here), add code to case 5 of the switch.
// • Clear the screen.
// • Generate a random Point2D point with an x, y anywhere in the console. This point will be the center position of the circle.
// • Calculate a random radius – ensure that it will NOT extend the circle beyond the bounds of the console.
// • Use the point and radius to create a Circle instance with any color you want.
// • Call Draw on the Circle instance.

Console.Clear();
rnd = new Random();
int x = rnd.Next(0, Console.WindowWidth-1);
int y = rnd.Next(0, Console.WindowHeight-1);
Point2D point = new Point2D(x, y);
int radius = rnd.Next();
while(x + radius > Console.WindowWidth || y + radius > Console.WindowHeight || x - radius < 0 || y - radius < 0)
{
radius = rnd.Next(1, Console.WindowWidth - x);
}
Circle circle = new Circle(radius, point, ConsoleColor.Magenta);
circle.Draw();
break;
// In Main (here), add code to case 5 of the switch.
// • Clear the screen.
// • Generate a random Point2D point with an x, y anywhere in the console. This point will be the center position of the circle.
// • Calculate a random radius – ensure that it will NOT extend the circle beyond the bounds of the console.
// • Use the point and radius to create a Circle instance with any color you want.
// • Call Draw on the Circle instance.

Console.Clear();
rnd = new Random();
int x = rnd.Next(0, Console.WindowWidth-1);
int y = rnd.Next(0, Console.WindowHeight-1);
Point2D point = new Point2D(x, y);
int radius = rnd.Next();
while(x + radius > Console.WindowWidth || y + radius > Console.WindowHeight || x - radius < 0 || y - radius < 0)
{
radius = rnd.Next(1, Console.WindowWidth - x);
}
Circle circle = new Circle(radius, point, ConsoleColor.Magenta);
circle.Draw();
break;
its an assignment I already turned in but would like some feedback as to why my check for radius boundary has been very wonky, i still had exceptions being thrown for it being out of bounds on my screen.
3 Replies
ChucklesTheBeard
ChucklesTheBeard15mo ago
Take a look at Math.Min() and Math.Max(). Can you think of a way to only generate a radius once, guaranteeing that it will fit? Also, what happens when x or y happens to be 0?
BI || CrescentThief
if x or y is zero then i would have to have the code rerun the generation of the x and y thanks for the insight
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.