Ejercicio
Matriz Bidimensional 2: Circunferencia En Pantalla
Objectivo
Cree un programa de C# que declare que crea una matriz bidimensional de caracteres de 70x20, "dibuja" una circunferencia o radio 8 dentro de ella y la muestra en la pantalla.
Sugerencia: los puntos en la circunferencia se pueden obtener utilizando:
x = xCenter + r * cos angle
y = yCenter + r * sin angle
"sin" y "cos" esperan que el ángulo se mida en radianes, en lugar de grados. Para convertir de una unidad a la otra, debe recordar que 360 grados = 2 radianes PI (o 180 grados = radianes PI): radianes flotantes = (ángulo * Math.PI / 180.0);
Puede dibujar 72 puntos (como hay 360 grados en una circunferencia, estarían espaciados 5 grados entre sí)
Sugerencia: en C#, el coseno es Math.Cos, el seno es Math.Sin y PI es Math.PI
Ejemplo Ejercicio C#
Mostrar Código C#
using System; // Importing the System namespace for accessing Console, Math, and other functionalities
class Program
{
static void Main()
{
// Declare a 70x20 two-dimensional array to represent the screen buffer
char[,] screenBuffer = new char[20, 70]; // 20 rows and 70 columns
// Fill the screen buffer with empty spaces initially
for (int row = 0; row < screenBuffer.GetLength(0); row++)
{
for (int col = 0; col < screenBuffer.GetLength(1); col++)
{
screenBuffer[row, col] = ' '; // Filling the array with empty spaces
}
}
// Define the center of the circle and the radius
int xCenter = 35; // Center column (x-coordinate) of the screen buffer
int yCenter = 10; // Center row (y-coordinate) of the screen buffer
int radius = 8; // Radius of the circle
// Draw the points of the circle using the parametric equations for a circle
for (int angle = 0; angle < 360; angle += 5) // Loop through 360 degrees with 5-degree increments
{
// Convert angle to radians
double radians = (angle * Math.PI / 180.0);
// Calculate the x and y coordinates of the point on the circumference
int x = (int)(xCenter + radius * Math.Cos(radians)); // Calculate x-coordinate
int y = (int)(yCenter + radius * Math.Sin(radians)); // Calculate y-coordinate
// Adjust the aspect ratio to compensate for the character's aspect ratio in the console
// The console characters are typically taller than they are wide, so we scale the x-coordinate
x = (int)(xCenter + radius * Math.Cos(radians) * 2); // Adjust x-coordinate for aspect ratio
// Place the point 'O' in the calculated position on the screen buffer
if (x >= 0 && x < screenBuffer.GetLength(1) && y >= 0 && y < screenBuffer.GetLength(0))
{
// Check if the point is already occupied (to avoid overwriting it)
if (screenBuffer[y, x] != 'O')
{
screenBuffer[y, x] = 'O'; // Mark the point on the screen buffer
}
}
}
// Display the content of the screen buffer (the array)
for (int row = 0; row < screenBuffer.GetLength(0); row++)
{
for (int col = 0; col < screenBuffer.GetLength(1); col++)
{
Console.Write(screenBuffer[row, col]); // Print each character in the array
}
Console.WriteLine(); // Move to the next line after each row is printed
}
}
}
Salida
OOO OO OOO OOO
OOO OOO
OO OO
O O
O O
O O
O O
O O
O OO
O O
O O
O O
O O
OO OO
OO O OOO
OOO OO OO OOO
O
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp