Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
1.1 kB
1
Indexable
Never
#include <stdio.h>
#include <graphics.h>
#include <math.h>

void draw_cube() {
  // Define the points of the cube.
  int points[][2] = {
      {100, 100}, {200, 100}, {200, 200}, {100, 200}, {100, 100}
  };

  // Draw the lines of the cube.
  for (int i = 0; i < 4; i++) {
    line(points[i][0], points[i][1], points[(i + 1) % 4][0], points[(i + 1) % 4][1]);
  }
}

void rotate_cube(float angle) {
  // Convert the angle to radians.
  angle = angle * M_PI / 180.0;

  // Rotate the points of the cube.
  for (int i = 0; i < 4; i++) {
    float x = points[i][0];
    float y = points[i][1];

    points[i][0] = x * cos(angle) - y * sin(angle);
    points[i][1] = x * sin(angle) + y * cos(angle);
  }
}

int main() {
  // Initialize the graphics system.
  initgraph(640, 480);

  // Get the angle of rotation from the user.
  float angle;
  printf("Enter the angle of rotation: ");
  scanf("%f", &angle);

  // Draw the cube.
  draw_cube();

  // Rotate the cube.
  rotate_cube(angle);

  // Draw the rotated cube.
  draw_cube();

  // Wait for the user to press a key.
  getch();

  // Close the graphics window.
  closegraph();

  return 0;
}