You are on page 1of 20

I HC M A CHT KHOA CNG NGH THNG TIN

Bi tp ln mn hc Ha My Tnh ti: Xy dng chng trnh v ha hai chiu

Nhm sinh vin thc hin Ng Th Lin Phm Th Ngn V Th Thm Phm c Thnh
0921050120 0921050275 0921050065 0921050154

Ging vin hng dn TS. Nguyn Quang Khnh

Page 1

Mc Lc
I. Li gii thiu ........................................................................................................... 3 II. Ni dung bo co .................................................................................................... 4 1) Thng tin cc thnh vin trong nhm ................................................................ 4 2) Bng phn chia cng vic ................................................................................... 4 3) Chc nng c xy dng trong chng trnh ................................................. 4 a) Chc nng trong chng trnh ............................................................................ 4 b) Lu thut ton ................................................................................................ 5 4) Gii thiu chc nng c bit trong chng trnh ............................................ 6 5) Mi trng v ngn ng s dng ................................................................. 6 6) Ci t chng trnh ........................................................................................... 7 a) Giao din ca chng trnh ................................................................................. 7 b) Ci t chng trnh ........................................................................................... 9 7) Danh sch th vin v ti liu tham kho ....................................................... 20

Page 2

I.

Li gii thiu
S pht trin ca khoa hc, k thut, ngh thut, kinh doanh v cng ngh lun lun ph thuc vo kh nng truyn t thng tin ca chng ta, hoc thng qua cc bit d liu lu tr trong microchip hoc thng qua giao tip bng ting ni. Cu chm ngn t xa xa mt hnh nh c gi tr hn c vn li" hay "trm nghe khng bng mt thy" cho thy ngha rt ln ca hnh nh trong vic chuyn ti thng tin. Hnh nh bao gi cng c cm nhn nhanh v d dng hn, c bit l trong trng hp bt ng v ngn ng. Do khng c g ngc nhin khi m ngay t khi xut hin my tnh, cc nh nghin cu c gng s dng n pht sinh cc nh trn mn hnh. Trong sut gn 50 nm pht trin ca my tnh, kh nng pht sinh hnh nh bng my tnh ca chng ta t ti mc m by gi hu nh tt c cc my tnh u c kh nng ha. ha my tnh l mt trong nhng lnh vc l th nht v pht trin nhanh nht ca tin hc. Ngay t khi xut hin, ha my tnh c sc li cun mnh lit, cun ht rt nhiu ngi nhiu lnh vc khc nhau nh khoa hc, ngh thut, kinh doanh, qun l, ... Tnh hp dn v a dng ca ha my tnh c th c minh ha rt trc quan thng qua vic kho st cc ng dng ca n. Ngy nay, ha my tnh c s dng trong rt nhiu lnh vc khc nhau nh cng nghip, thng mi, qun l, gio dc, gii tr, S lng cc chng trnh ha ng dng tht khng l v pht trin lin tc.Trong lnh vc cng nghip,CAD(computeraided design) c s dng hu ht trong vic thit k cc cao c, t, my bay, tu thy, tu v tr, my tnh,Trong lnh vc gii tr,ngh thut, ha my tnh gip ta to ra cc chng trnh tr chi,cc k xo in nh cho cc nh lm phim,hay ngay chnh giao din ha lm nng cao kh nng giao tip gia ngi v my tnh. c th lm c nhng ng dng ha hu ch cho cuc sng,trc ht chng ta cn phi c mt nn tng vng chc v lp trnh ha.C rt nhiu ngn ng h tr lp trnh ha my tnh,mi ngn ng c xy dng trn nhng th vin ha ring,c nhng th mnh ring.V khi ni v lp trnh ha,chng ta khng th khng ni n ngn ng lp trnh C#,mt ngn ng rt mnh trong lnh vc ny.Vi mc ch nghin cu,tm hiu v mang n cho nhng ai mun tm hiu v lp trnh ha mt ti liu thit thc,nhm chng em xy dng chng trnh v ha hai chiu . V l ln thi gian lm bi tng i ngn, kin thc cn hn ch nn chng em khng trnh khi sai st.Chng em mong thy xem xt v gp cho ti liu ny.Chng em chn thnh cm n.

Page 3

II.

Ni dung bo co
1) Thng tin cc thnh vin trong nhm
STT 1 2 3 4 H v Tn Ng Th Lin Phm Th Ngn V Th Thm Phm c Thnh MSSV 0921050120 0921050275 0921050065 0921050154 01696884092 01649711431 01656100343 miss91.it@mail.com Thamvt91@gmail.com mrlordkaj@gmail.com in thoi Email

2) Bng phn chia cng vic


STT 1 H v tn sinh vin thc hin Ng Th Lin Ni dung thc hin Thit k giao din. Tm hiu thut ton,ng dng v cc i tng ha 2 chiu Tm hiu thut ton,ng dng v cc i tng ha 2 chiu . Tester. Tm hiu, xy dng chc nng bin hnh cho cc i tng ha. Tng hp,vit ti liu Tng hp, hon thin cc chc nng v ha c s. Bt v x l s kin trn form. Xy dng th vin hnh, cung cp chc nng cho ngi dng thao tc vi th vin hnh. Gom nhm v phn r cc i tng ha.

Phm Th Ngn

V Th Thm

Phm c Thnh

3) Chc nng c xy dng trong chng trnh


a) Chc nng trong chng trnh Cho ngi dng t v cc i tng ha hai chiu trn giao din ca chng trnh:

Page 4

o V i tng ng thng, ng gp khc. o V i tng ng trn, eclipse, cung trn. o V hnh ch nht, hnh a gic. Cho php ngi dng thay i cc thuc tnh ca i tng: o i ng vin, mu vin. o Tng rng ca kch thc ng vin. o Cho php ngi dng t mu nhng hnh c ng khp kn (hnh trn, a gic, eclipse). Cho php ngi dng lu tr bn v dng bitmap, m i tng bn v nh dng bitmap. Cho php ngi dng s dng th vin hnh c sn trong chng trnh, ngoi ra cn c th thm cc i tng vo th vin hnh. C cc chc nng bin hnh cho cc i tng ha o Php tnh tin i tng ha o Php thay i t l ca i tng ha. o Php quay i tng theo tm 0 hoc theo tm bt k. o Php bin dng hnh theo trc x, trc y.

b) Lu thut ton Thut ton Bresenham v ng thng

Page 5

Thut ton MidPoint v ng trn

4) Gii thiu chc nng c bit trong chng trnh


Cho php ngi dng to mt i tng ha bt k t cc i tng c s sau lu vo th vin hnh ca chng trnh. To cc th vin hnh c sn cc i tng (hnh sao, mi tn,). Cho php ngi dng s dng cc i tng c sn trong th vin. Cho php chn cc thuc tnh ca bt v ( rng ca bt, kiu ng vin ca bt v, mu ca bt v,). Lu cc i tng v,m cc i tng ha c sn. Khi v cc i tng ha,cho php xem li cc i tng c s v, c th xa cc i tng bt k trong .

5) Mi trng v ngn ng s dng


S dng mi trng visual studio 2010 xy dng chng trnh. S dng ngn ng lp trnh C#.

Page 6

6) Ci t chng trnh
a) Giao din ca chng trnh Giao din chnh ca chng trnh:

Giao din kt qu khi v i tng ng thng,ng gp khc

Giao din kt qu khi v ng cong, ng trn, ellipse

Page 7

Giao din khi v a gic

Giao din khi gom nhm cc i tng, to thnh hnh phc hp, lu vo th vin i tng ca chng trnh

Page 8

b) Ci t chng trnh
b.1. Ci t thut ton v cc i tng ha 2 chiu

b.1.1. i tng ng thng


class DrawLine : DrawObject { public DrawLine(Point _start) { point.Add(_start); point.Add(_start); } public DrawLine(Point _start, Point _end) { point.Add(_start); point.Add(_end); } public override void Update(Point p, Graphics g, Bitmap d) { point[1] = p; s.X = Math.Min(point[0].X, point[1].X); s.Y = Math.Min(point[0].Y, point[1].Y); e.X = Math.Max(point[0].X, point[1].X); e.Y = Math.Max(point[0].Y, point[1].Y); Bitmap drawArea = (Bitmap)d.Clone(); Draw(drawArea); g.DrawImage(drawArea, new Point(0, 0)); }

public override void Draw(Bitmap drawArea) { Graphics g = Graphics.FromImage(drawArea); g.DrawLine(pen, point[0], point[1]); if (selected) Select(g); } //Thuat toan Bresenham: http://vi.wikipedia.org/wiki/Gi%E1%BA%A3i_thu%E1%BA%ADt_Bresenham_v%E1%BA%BD_%C4%9 1o%E1%BA%A1n_th%E1%BA%B3ng public void Draw(Graphics g, Bitmap drawArea) { int x0 = point[0].X, y0 = point[0].Y; int x1 = point[1].X, y1 = point[1].Y; int dX = Math.Abs(x1 - x0); int dY = Math.Abs(y1 - y0); int sX, sY; if (x0 < x1) sX = 1; else sX = -1; if (y0 < y1) sY = 1; else sY = -1; int err = dX - dY; while (x0 != x1 || y0 != y1) { drawArea.SetPixel(x0, y0, pen.Color);

Page 9

int e2 = 2 * err; if (e2 > -dY) { err -= dY; x0 += sX; } if (e2 < dX) { err += dX; y0 += sY; } } } }

b.1.2. i tng hnh trn


class DrawCircle : DrawObject { private int r; public DrawCircle(Point _o) { point.Add(_o); r = 0; } public DrawCircle(Point _o, int _r) { point.Add(_o); r = _r; } public override void Update(Point p, Graphics g, Bitmap d) { int dX = p.X - point[0].X; int dY = p.Y - point[0].Y; r = (int)Math.Sqrt((double)(dX * dX + dY * dY)); s.X = point[0].X - r; s.Y = point[0].Y - r; e.X = point[0].X + r; e.Y = point[0].Y + r; Bitmap drawArea = (Bitmap)d.Clone(); Draw(drawArea); g.DrawImage(drawArea, new Point(0, 0)); } public override void Draw(Bitmap drawArea) { Graphics g = Graphics.FromImage(drawArea); Rectangle rec = new Rectangle(); rec.X = point[0].X - r; rec.Y = point[0].Y - r; rec.Width = r * 2; rec.Height = r * 2; if (fill != Brushes.Transparent) g.FillEllipse(fill, rec); g.DrawEllipse(pen, rec); if (selected) Select(g);

Page 10

} //Thuat toan MidPoint xem trong sach public void Draw(Graphics g, Bitmap drawArea) { int x = 0, y = r; Put8Pixel(x, y, drawArea); int p = 1 - r; //5/4 - r; while (x < y) { if (p < 0) p += 2 * x + 3; else { p += 2 * (x - y) + 5; y--; } x++; Put8Pixel(x, y, drawArea); } } private void Put8Pixel(int x, { int oX = point[0].X, oY = drawArea.SetPixel(oX + x, drawArea.SetPixel(oX + y, drawArea.SetPixel(oX + y, drawArea.SetPixel(oX + x, drawArea.SetPixel(oX - x, drawArea.SetPixel(oX - y, drawArea.SetPixel(oX - y, drawArea.SetPixel(oX - x, } } int y, Bitmap drawArea) point[0].Y; oY + y, pen.Color); oY + x, pen.Color); oY - x, pen.Color); oY - y, pen.Color); oY - y, pen.Color); oY - x, pen.Color); oY + x, pen.Color); oY + y, pen.Color);

b.1.3. i tng v Ellipse


class DrawEllipse : DrawObject { public DrawEllipse(Point _start) { point.Add(_start); point.Add(_start); } public DrawEllipse(Point _start, Point _end) { point.Add(_start); point.Add(_end); } public override void Update(Point p, Graphics g, Bitmap d) { s = point[0]; e = point[1] = p; Bitmap drawArea = (Bitmap)d.Clone(); Draw(drawArea); g.DrawImage(drawArea, new Point(0, 0));

Page 11

} public override void Draw(Bitmap drawArea) { Graphics g = Graphics.FromImage(drawArea); Rectangle rec = new Rectangle(); rec.X = Math.Min(point[0].X, point[1].X); rec.Y = Math.Min(point[0].Y, point[1].Y); rec.Width = Math.Abs(point[0].X - point[1].X); rec.Height = Math.Abs(point[0].Y - point[1].Y); if (fill != Brushes.Transparent) g.FillEllipse(fill, rec); g.DrawEllipse(pen, rec); if (selected) Select(g); } }

b.1.4. i tng v a gic


class DrawPolygon : DrawObject { public DrawPolygon(Point _start) { point.Add(_start); point.Add(_start); s = e = _start; } public DrawPolygon(List<Point> _p) { point = _p; s = e = _p[0]; for (int i = 1; i < _p.Count; i++) { if (_p[i].X < s.X) s.X = _p[i].X; if (_p[i].Y < s.Y) s.Y = _p[i].Y; if (_p[i].X > e.X) e.X = _p[i].X; if (_p[i].Y > e.Y) e.Y = _p[i].Y; } } public void AddPoint(Point _point) { point[point.Count - 1] = _point; point.Add(_point); if (_point.X < s.X) s.X = _point.X; if (_point.Y < s.Y) s.Y = _point.Y; if (_point.X > e.X) e.X = _point.X; if (_point.Y > e.Y) e.Y = _point.Y; } public override void Update(Point p, Graphics g, Bitmap d) { point[point.Count - 1] = p; Bitmap drawArea = (Bitmap)d.Clone(); Draw(drawArea); g.DrawImage(drawArea, new Point(0, 0)); }

Page 12

public override void Draw(Bitmap drawArea) { Graphics g = Graphics.FromImage(drawArea); if (fill != Brushes.Transparent) g.FillPolygon(fill, point.ToArray()); g.DrawPolygon(pen, point.ToArray()); if (selected) Select(g); } }

b.2. Code chc nng cho php ko th v cc i tng

Khi bt u vo chng trnh, bt u chn cc i tng mun s dng (ng thng, ng gp khc,ng trn,ng elipse, a gic,) trn menu chnh ca chng trnh. Khung v i tng l mt picturebox, thao tc v cc i tng trn khung ny. Bt u ly ta u ca cc i tng:
private void SelectTool(Tool select) { btnLine.CheckState = CheckState.Unchecked; btnRectangle.CheckState = CheckState.Unchecked; btnBLine.CheckState = CheckState.Unchecked; btnPolygon.CheckState = CheckState.Unchecked; btnCircle.CheckState = CheckState.Unchecked; btnArc.CheckState = CheckState.Unchecked; btnEllipse.CheckState = CheckState.Unchecked; if (tool != select) { tool = select; pnlDraw.Cursor = Cursors.Cross; } else { tool = Tool.None; pnlDraw.Cursor = Cursors.Default; } switch (tool) { case Tool.Line: btnLine.CheckState = CheckState.Checked; break; case Tool.Rectangle: btnRectangle.CheckState = CheckState.Checked; break; case Tool.BLine: btnBLine.CheckState = CheckState.Checked; break; case Tool.Polygon: btnPolygon.CheckState = CheckState.Checked; break; case Tool.Circle:

Page 13

btnCircle.CheckState = CheckState.Checked; break; case Tool.Arc: btnArc.CheckState = CheckState.Checked; break; case Tool.Ellipse: btnEllipse.CheckState = CheckState.Checked; break; } } else { DrawBLine curBLine = (DrawBLine)objectList.Last(); curBLine.AddPoint(e.Location); } } else if (drawing) { DrawBLine curBLine = (DrawBLine)objectList.Last(); curBLine.point.RemoveAt(curBLine.point.Count - 1); Graphics g = pnlDraw.CreateGraphics(); curBLine.Update(curBLine.point.Last(), g, drawArea); DoneDraw("BLine"); drawing = false; } break; case Tool.Polygon: //Chn to ban u V a gic if (e.Button == MouseButtons.Left) { if (!drawing) { objectList.Add(new DrawPolygon(e.Location)); drawing = true; } else { DrawPolygon curPolygon = (DrawPolygon)objectList.Last(); curPolygon.AddPoint(e.Location); } } else if (drawing) { DrawPolygon curPolygon = (DrawPolygon)objectList.Last(); curPolygon.point.RemoveAt(curPolygon.point.Count - 1); Graphics g = pnlDraw.CreateGraphics(); curPolygon.Update(curPolygon.point.Last(), g, drawArea); DoneDraw("Polygon"); drawing = false; } break; case Tool.Circle: //Chn to ban u V

dg trn

Page 14

if (!drawing) objectList.Add(new DrawCircle(e.Location)); else DoneDraw("Circle"); drawing = !drawing; break; case Tool.Arc: if (!drawing) { objectList.Add(new DrawArc(e.Location)); drawing = true; } else { DrawArc curArc = (DrawArc)objectList.Last(); if (!curArc.adjushAngle) { curArc.adjushAngle = true; curArc.lastX = e.X; } else { curArc.adjushAngle = false; DoneDraw("Arc"); drawing = false; } } break; case Tool.Ellipse: //Chn to ban u V elipse if (!drawing) objectList.Add(new DrawEllipse(e.Location)); else DoneDraw("Ellipse"); drawing = !drawing; break; } }

Sau thc hin thao tc ko th chut v i tng


private void pnlDraw_MouseMove(object sender, MouseEventArgs e) { lblCoordinate.Text = e.X + "x" + e.Y; if (drawing) { switch (tool) { case Tool.Line: DrawLine newLine = (DrawLine)objectList.Last(); newLine.Update(e.Location, drawGraphic, drawArea); break; case Tool.Rectangle: DrawRectangle newRectangle = (DrawRectangle)objectList.Last(); newRectangle.Update(e.Location, drawGraphic, drawArea); break; case Tool.BLine: DrawBLine newBLine = (DrawBLine)objectList.Last();

Page 15

newBLine.Update(e.Location, drawGraphic, drawArea); break; case Tool.Polygon: DrawPolygon newPolygon = (DrawPolygon)objectList.Last(); newPolygon.Update(e.Location, drawGraphic, drawArea); break; case Tool.Circle: DrawCircle newCircle = (DrawCircle)objectList.Last(); newCircle.Update(e.Location, drawGraphic, drawArea); break; case Tool.Arc: DrawArc newArc = (DrawArc)objectList.Last(); if (newArc.adjushAngle) { newArc.angle = Math.Abs(360 - Math.Abs(newArc.lastX - e.X)*2); } newArc.Update(e.Location, drawGraphic, drawArea); break; case Tool.Ellipse: DrawEllipse newEllipse = (DrawEllipse)objectList.Last(); newEllipse.Update(e.Location, drawGraphic, drawArea); break; } } if (e.Button == System.Windows.Forms.MouseButtons.Left && tool == Tool.None && lstObject.SelectedItems.Count > 0) { for (int i = 0; i < lstObject.SelectedItems.Count; i++) { DrawObject a = (DrawObject)objectList[lstObject.SelectedItems[i].Index]; if (a.GetType().Name != "DrawGroup") { for (int j = 0; j < a.point.Count; j++) { Point p = a.point[j]; p.X += e.X - curPos.X; p.Y += e.Y - curPos.Y; a.point[j] = p; } } else { DrawGroup d = (DrawGroup)a; d.Move(e.X - curPos.X, e.Y - curPos.Y); } a.s.X += e.X - curPos.X; a.s.Y += e.Y - curPos.Y; a.e.X += e.X - curPos.X; a.e.Y += e.Y - curPos.Y; curPos = e.Location; } lblTest.Text = (e.X - curPos.X).ToString(); RefreshDraw();

Page 16

} else { lblTest.Text = "false"; } }

Thc hin v cc i tng khi nhn chut phi:


private void DoneDraw(string type) { ListViewItem newItem = new ListViewItem(type); newItem.SubItems.Add(DateTime.Now.ToString()); lstObject.Items.Add(newItem); drawArea = new Bitmap(background); foreach (DrawObject d in objectList) d.Draw(drawArea); SelectTool(Tool.None); pnlDraw.Cursor = Cursors.Default; }

b.3. Chc nng nng cao cho php gom nhm cc i tng, to th vin hnh

b.3.1. Nhm cc i tng c s


public void SaveLibrary(string alias, string title) { int id = lstObject.SelectedItems[0].Index; DrawGroup group = (DrawGroup)objectList[id]; FileStream file = new FileStream(@"lib\" + alias + ".lib", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter writer = new StreamWriter(file, Encoding.Unicode); writer.WriteLine(title); writer.WriteLine(group.objects.Count.ToString()); for (int i = 0; i < group.objects.Count; i++) { string row = group.objects[i].GetType().Name + ";"; for (int j = 0; j < group.objects[i].point.Count; j++) { row += group.objects[i].point[j].X + "," + group.objects[i].point[j].Y + ";"; } writer.WriteLine(row.Substring(0, row.Length - 1)); } writer.Close(); file.Close(); LoadLibrary(); } private void mnuGroup_Click(object sender, EventArgs e) { List<DrawObject> objects = new List<DrawObject>(); for (int i = objectList.Count - 1; i >= 0; i--) { if (lstObject.Items[i].Selected) { objectList[i].selected = false; if (lstObject.Items[i].Text == "Group")

Page 17

{ DrawGroup curGroup = (DrawGroup)objectList[i]; objects.AddRange(curGroup.objects); } else objects.Add(objectList[i]); objectList.RemoveAt(i); lstObject.Items.RemoveAt(i); } } DrawGroup group = new DrawGroup(objects); objectList.Add(group); DoneDraw("Group"); RefreshDraw(); }

Lu i tng vo th vin hnh:


private void mnuSaveGroup_Click(object sender, EventArgs e) { SaveLibrary slForm = new SaveLibrary(this); slForm.Show(); }

Xa cc i tng va gom nhm:


private void mnuDelete_Click(object sender, EventArgs e) { for (int i = objectList.Count - 1; i >= 0; i--) { if (lstObject.Items[i].Selected) { objectList.RemoveAt(i); lstObject.Items.RemoveAt(i); } } RefreshDraw(); }

b.3.2. Thay i cc thuc tnh ca i tng ha Thay i rng ca bt v:


private void cboBorderWidth_SelectedIndexChanged(object sender, EventArgs e) { int selectedId = lstObject.SelectedItems[0].Index; objectList[selectedId].pen.Width = int.Parse(cboBorderWidth.Text); RefreshDraw(); }

Thay i mu ca bt v:
private void picBorderColor_Click(object sender, EventArgs e) { int selectedId = lstObject.SelectedItems[0].Index; clrPicker.ShowDialog(); objectList[selectedId].pen.Color = picBorderColor.BackColor = clrPicker.Color; RefreshDraw();

Page 18

Thay i ng vin ca bt v ( v bng ng nt lin, nt t, dotted):


private void cboBorderType_SelectedIndexChanged(object sender, EventArgs e) { int selectedId = lstObject.SelectedItems[0].Index; switch (cboBorderType.SelectedIndex) { case 0: objectList[selectedId].pen.DashStyle = DashStyle.Solid; break; case 1: objectList[selectedId].pen.DashStyle = DashStyle.Dash; break; case 2: objectList[selectedId].pen.DashStyle = DashStyle.Dot; break; } RefreshDraw(); }

mu cho cc i tng l hnh khp kn:


private void picFillColor_Click(object sender, EventArgs e) { int selectedId = lstObject.SelectedItems[0].Index; clrPicker.ShowDialog(); objectList[selectedId].fill.Color = picFillColor.BackColor = clrPicker.Color; RefreshDraw(); }

b.4. Gi chc nng khi c s kin xy ra


private void btnEllipse_Click(object sender, EventArgs e) { SelectTool(Tool.Ellipse); } private void btnArc_Click(object sender, EventArgs e) { SelectTool(Tool.Arc); } private void btnPolygon_Click(object sender, EventArgs e) { SelectTool(Tool.Polygon); } private void btnLine_Click(object sender, EventArgs e) { SelectTool(Tool.Line); } private void btnRectangle_Click(object sender, EventArgs e) { SelectTool(Tool.Rectangle); }

Page 19

private void btnBLine_Click(object sender, EventArgs e) { SelectTool(Tool.BLine); } private void btnCircle_Click(object sender, EventArgs e) { SelectTool(Tool.Circle); }

7) Danh sch th vin v ti liu tham kho


Bi ging ha my tnh GV. Nguyn Quang Khnh. Gio trnh ha my tnh. Thy Dng Anh c, L nh Huy. Khoa cng ngh thng tin, Trng H Khoa hc t nhin. Gio trnh ha vi tnh. Thy Nguyn Quc Cng, Hong c Hi. Nh xut bn gio dc. Gio trnh k thut ha. Thy L Tun Hng, Hunh Quyt Thng. i Hc Bch Khoa H Ni.

Page 20