Programming Course in C# ¡Free!

Two-dimensional array 2: circunference on screen

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

"sin" and "cos" expect the angle to be measured in radians, instead of degrees. To convert from one unit to the other, you must remember that 360 degrees = 2 PI radians (or 180 degrees = PI radians): float radians = (angle * Math.PI / 180.0);

You might draw 72 points (as there are 360 degrees in a circumference, they would be spaced 5 degreees from each other)


Hint: in C#, cosine is Math.Cos, sine is Math.Sin and PI is Math.PI

Output



Solution


using System;

public class Circunference
{   
public static void Main ()
{
double r, x, y;

for(int i = 0; i < 360; i+=5)
{
rad = i * Math.PI / 180.0;

x = 35 + 8 * Math.Cos(rad);
y = 10 + 8 * Math.Sin(rad);

Console.SetCursorPosition( (int) x, (int) y); 
Console.Write("X");   
}

Console.SetCursorPosition(1, 20); 
}
}