⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3d.c

📁 Many C samples. It is a good sample for students to learn C language.
💻 C
字号:
/* Bai tap 2_31 - Hien thi 3D (khong gian ba chieu) */
#include <conio.h>
#include <graphics.h>
#include <math.h>

typedef struct {
  double x, y, z;
} Point3D;

double yaw, roll, pitch, depth;
static Point3D p[] = {
{-35.0, -35.0, 35.0},
{35.0, -35.0, 35.0},
{35.0, 35.0, 35.0},
{-35.0, 35.0, 35.0},
{-35.0, -35.0, 35.0},
{-35.0, -35.0, -35.0},
{35.0, -35.0, -35.0},
{35.0, 35.0, -35.0},
{-35.0, 35.0, -35.0},
{-35.0, -35.0, -35.0}
};

void show_3d(Point3D p[], int n)
{
  double sinyaw, cosyaw, sinroll, cosroll, sinpitch, cospitch,
         yawx, yawy, rollx, rollz, pitchx, pitchy, pitchz, sx, sy;
  int r;

  cleardevice();

  sinyaw = sin(yaw);
  cosyaw = cos(yaw);
  sinroll = sin(roll);
  cosroll = cos(roll);
  sinpitch = sin(pitch);
  cospitch = cos(pitch);

  for (r=0; r<n; r++)
  {
    yawx = (cosyaw * p[r].x) - (sinyaw * p[r].y);
    yawy = (sinyaw * p[r].x) + (cosyaw * p[r].y);
    rollx = (cosroll * yawx) + (sinroll * p[r].z);
    rollz = (cosroll * p[r].z) - (sinroll * p[r].z);
    pitchy = (cospitch * yawy) - (sinpitch * rollz);
    pitchz = (sinpitch * yawy) + (cospitch * rollz);

    sx = depth * (rollx+5) / (pitchy+250);
    sy = depth * (pitchz-5) / (pitchy+250);
    if (r == 0)
      moveto(sx+getmaxx() / 2, sy + getmaxy() / 2);
    else
      lineto(sx+getmaxx() / 2, sy+getmaxy() / 2);
  }
}

void main()
{
  int gr_drive = DETECT, gr_mode, c=0;

  initgraph(&gr_drive, &gr_mode, "");

  yaw = 5.7;
  roll = 6.3;
  pitch = 5.9;
  depth = 360;
  while (c != 27)
  {
    show_3d(p, 10);
    c = getch();
    if (c==0)
      c = getch();
    switch (c)
    {
      case 75 : yaw -= 0.1;
                break;
      case 77 : yaw += 0.1;
                break;
      case 72 : pitch -= 0.1;
                break;
      case 80 : pitch += 0.1;
                break;
    }
  }
  closegraph();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -