trukka
struct Point3D
{
public float X, Y, Z;
public Point3D(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
}
struct wireframe
{
public Point3D[] vert;
public int[,] edge;
}
public partial class Form1 : Form
{
wireframe wtru, wnon;
Graphics g;
int chon = 1;
public Form1()
{
InitializeComponent();
g = this.CreateGraphics();
}
void VeWOXY(wireframe w, Graphics g, int W, int H, Color c)
{
Point3D p1, p2;
PointF Q1, Q2;
for (int i = 0; i < w.edge.GetLength(0); i++)
{
p1 = w.vert[w.edge[i, 0]];
p2 = w.vert[w.edge[i, 1]];
Q1 = ChieuSSOXY(p1);
Q2 = ChieuSSOXY(p2);
Myline(g, W, H, Q1, Q2, c);
}
}
void VeWOYZ(wireframe w, Graphics g, int W, int H, Color c)
{
Point3D p1, p2;
PointF Q1, Q2;
for (int i = 0; i < w.edge.GetLength(0); i++)
{
p1 = w.vert[w.edge[i, 0]];
p2 = w.vert[w.edge[i, 1]];
Q1 = ChieuSSOYZ(p1);
Q2 = ChieuSSOYZ(p2);
Myline(g, W, H, Q1, Q2, c);
}
}
void VeWOZX(wireframe w, Graphics g, int W, int H, Color c)
{
Point3D p1, p2;
PointF Q1, Q2;
for (int i = 0; i < w.edge.GetLength(0); i++)
{
p1 = w.vert[w.edge[i, 0]];
p2 = w.vert[w.edge[i, 1]];
Q1 = ChieuSSOZX(p1);
Q2 = ChieuSSOZX(p2);
Myline(g, W, H, Q1, Q2, c);
}
}
void Myline(Graphics g, int W, int H, PointF p1, PointF p2, Color c)
{
p1.Y = H - p1.Y;
p1.X += W;
p2.Y = H - p2.Y;
p2.X += W;
g.DrawLine(new Pen(Color.Red), p1, p2);
}
PointF ChieuSSOXY(Point3D p)
{
PointF Q = new PointF();
Q.X = p.X;
Q.Y = p.Y;
return Q;
}
PointF ChieuSSOYZ(Point3D p)
{
PointF Q = new PointF();
Q.Y = p.Y;
Q.X = p.Z;
return Q;
}
PointF ChieuSSOZX(Point3D p)
{
PointF Q = new PointF();
Q.X = p.X;
Q.Y = p.Z;
return Q;
}
Point3D XoayOx(Point3D p, float al)
{
Point3D q = new Point3D();
q.X = p.X;
q.Y = (float)(p.Y * Math.Cos(al) - p.Z * Math.Sin(al));
q.Z = (float)(p.Y * Math.Sin(al) + p.Z * Math.Cos(al));
return q;
}
void XoayWOx(ref wireframe w, float al)
{
for (int i = 0; i < w.vert.Length; i++)
{
w.vert[i] = XoayOx(w.vert[i], al);
}
}
Point3D XoayOy(Point3D p, float al)
{
Point3D q = new Point3D();
q.Y = p.Y;
q.X = (float)(p.Z * Math.Sin(al) + p.X * Math.Cos(al));
q.Z = (float)(p.Z * Math.Cos(al) - p.X * Math.Sin(al));
return q;
}
void XoayWOy(ref wireframe w, float al)
{
for (int i = 0; i < w.vert.Length; i++)
{
w.vert[i] = XoayOy(w.vert[i], al);
}
}
Point3D XoayOz(Point3D p, float al)
{
Point3D q = new Point3D();
q.Z = p.Z;
q.Y = (float)(p.X * Math.Sin(al) + p.Y * Math.Cos(al));
q.X = (float)(p.X * Math.Cos(al) - p.Y * Math.Sin(al));
return q;
}
void XoayWOz(ref wireframe w, float al)
{
for (int i = 0; i < w.vert.Length; i++)
{
w.vert[i] = XoayOz(w.vert[i], al);
}
}
private void Form1_Load(object sender, EventArgs e)
{
wtru = HinhTru(150, 50, 20);
wnon = HinhNon(100, 50, 20);
}
wireframe HinhTru (float r, float l, int n)
{
wireframe w;
w.vert = new Point3D[2 * n];
w.edge = new int[3 * n, 2];
double u = 0, du = 2 * Math.PI / n;
// m,at day tren
for (int i = 0; i < n; i++)
{
w.vert[i].X = (float)(r * Math.Cos(u));
w.vert[i].Y = (float)(r * Math.Sin(u));
w.vert[i].Z = l / 2;
u += du;
}
//double u = 0; mat day duoi
for (int i = n; i < 2*n; i++)
{
w.vert[i].X = (float)(r * Math.Cos(u));
w.vert[i].Y = (float)(r * Math.Sin(u));
w.vert[i].Z = -l / 2;
u += du;
}
// canh xung quanh
for (int i = 0; i < n; i++)
{
w.edge[i, 0] = i;
w.edge[i, 1] = i + n;
}
// canh day tren
for (int i = 0; i < n-1; i++)
{
w.edge[i+n, 0] = i;
w.edge[i+n, 1] = i + 1;
}
w.edge[2 * n - 1, 0] = n - 1;
w.edge[2 * n - 1, 1] = 0;
// canh day duoi
for (int i = 0; i < n -1; i++)
{
w.edge[i + 2*n, 0] = i+n;
w.edge[i + 2*n, 1] = i +n+ 1;
}
w.edge[3 * n - 1, 0] = 2 * n- 1;
w.edge[3* n - 1, 1] = n;
return w;
}
wireframe HinhNon(float z0, float r, int n)
{
wireframe w;
w.vert = new Point3D[n+1];
w.edge = new int[2 * n, 2];
double u = 0, du = 2 * Math.PI / n;
w.vert[0] = new Point3D(0, 0, z0);
for (int i = 0; i < n; i++)
{
w.vert[i + 1] = new Point3D((float)(r * Math.Cos(u)), (float)(r * Math.Sin(u)),0);
u+=du;
}
for (int i = 0; i < n; i++)
{
w.edge[i,0]=0;
w.edge[i,1]=i+1;
}
for (int i = 0; i < n-1; i++)
{
w.edge[i+n,0]=i+1;
w.edge[i+n,1]=i+2;
}
w.edge[2*n-1,0]=1;
w.edge[2*n-1,1]=n;
return w;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (chon==1)
VeWOXY(wtru, e.Graphics, panel1.Width / 2, panel1.Height / 2, Color.Red);
else
VeWOXY(wnon, e.Graphics, panel1.Width / 2, panel1.Height / 2, Color.Red);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.X:
{
XoayWOx(ref wtru, (float)Math.PI/180);
XoayWOx(ref wnon, (float)Math.PI / 180);
break;
}
case Keys.Y:
{
XoayWOy(ref wtru, (float)Math.PI / 180);
XoayWOx(ref wnon, (float)Math.PI / 180);
break;
}
case Keys.Z:
{
XoayWOz(ref wtru, (float)Math.PI / 180);
XoayWOx(ref wnon, (float)Math.PI / 180);
break;
}
}
panel1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
chon = 1;
panel1.Invalidate();
}
private void button2_Click(object sender, EventArgs e)
{
chon = 2;
panel1.Invalidate();
}
Bạn đang đọc truyện trên: AzTruyen.Top