baitapc31
namespace baitapc31
{
public partial class Form1 : Form
{
Graphics g;
Bitmap b;
Pen pen;
public struct point2D
{
public double x, y;
}
public Form1()
{
InitializeComponent();
b = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
g = Graphics.FromImage(b);
pen=new Pen(Color.Black);
}
public void drawpolygon(point2D []p)
{
for (int i = 0; i < p.Length-1; i++)
{
myline(p[i], p[i + 1]);
}
myline(p[p.Length - 1], p[0]);
}
public void myline(point2D p1, point2D p2)
{
double y1 = b.Height - p1.y;
double y2 = b.Height - p2.y;//chuyen ve toa do duoi goc trai
g.DrawLine(pen ,(float)p1.x, (float)y1, (float)p2.x, (float)y2);
}
public void Tinhtiendiem(ref double x,ref double y, double tx, double ty)
{
x += tx;
y += ty;
}
public void TinhtienDsdiem(point2D[]p, double tx, double ty)
{//tu viet
for(int i=0;i<p.Length;i++)
{
Tinhtiendiem(ref p[i].x, ref p[i].y, tx, ty);
}
}
public void xoaydiem(ref double x, ref double y, double alpha)
{
alpha = Math.PI * alpha / 180;
double x1 = Math.Cos(alpha) * x - Math.Sin(alpha) * y;//cong them 1 phan nua neu xoay theo tam c
double y1 = Math.Sin(alpha) * x + Math.Cos(alpha) * y;
x = x1; y = y1;
}
public void xoaydiemtoadobatky(ref double x, ref double y, double alpha,double incx,double incy)
{
alpha = Math.PI * alpha / 180;
double x1 = Math.Cos(alpha) * x - Math.Sin(alpha) * y+(1-Math.Cos(alpha))*incx+Math.Sin(alpha)*incy;//cong them 1 phan nua neu xoay theo tam c
double y1 = Math.Sin(alpha) * x + Math.Cos(alpha) * y - Math.Sin(alpha) * incx + (1 - Math.Cos(alpha)) * incy;
x = x1; y = y1;
}
public void xoayDsdiem(point2D[] p, double al)
{
for (int i = 0; i < p.Length; i++)
{
xoaydiem(ref p[i].x, ref p[i].y,al);
}
}
public void xoayDsdiemtoadobaky(point2D[] p, double al, double incx, double incy)
{
for (int i = 0; i < p.Length; i++)
{
xoaydiemtoadobatky(ref p[i].x, ref p[i].y, al,incx,incy);
}
}
public void Phongto(ref double x, ref double y, double sx, double sy)
{
x *= sx;
y *= sy;
}
public void PhongtoDsdiem(point2D[] p, double sx, double sy)
{
for (int i = 0; i < p.Length; i++)
{
Phongto(ref p[i].x, ref p[i].y, sx, sy);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
point2D []vert;
vert = new point2D[4];
vert[0].x = 100; vert[0].y = 100;
vert[1].x = 200; vert[1].y = 100;
vert[2].x = 200; vert[2].y = 200;
vert[3].x = 100; vert[3].y = 200;
drawpolygon(vert);
TinhtienDsdiem(vert, 100, 100);
drawpolygon(vert);
PhongtoDsdiem(vert, 1.5, 1.5);
drawpolygon(vert);
xoayDsdiemtoadobaky(vert, 45, 300, 300);
drawpolygon(vert);
Graphics gr = e.Graphics;
gr.DrawImageUnscaled(b, 0, 0);
}
}
}
Bạn đang đọc truyện trên: AzTruyen.Top