Cơ sở toán học trong đồ hoạ máy tính

Lời nói đầu Chương 1. Tổng quan về đồ họa máy tính Chương 2. Sơ sở toán học của các hệ trục tọa độ & những phương thức hiện thực trong đồ họa máy tính Chương 3. Cơ sở toán học của các đối tượng đồ họa & việc hiện thực trong môi trường windows Chương 4. Các giải thuật tô màu Chương 5. Hệ patterns Chương 6. Ngôn ngữ Visual C++ Chương trình nguồn

pdf117 trang | Chia sẻ: lvcdongnoi | Lượt xem: 2550 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Cơ sở toán học trong đồ hoạ máy tính, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
eå veõ hình chöõ nhaät bao beân ngoaøi void CPatternsView::RectangleReal(CDC* pDC, double x1, double y1, double x2, double y2) { PointType A; SetPoint(x1,y1,&A); MoveToReal(pDC,A); SetPoint(x2,y1,&A); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 76 LineToReal(pDC,A); SetPoint(x2,y2,&A); LineToReal(pDC,A); SetPoint(x1,y2,&A); LineToReal(pDC,A); SetPoint(x1,y1,&A); LineToReal(pDC,A); } Töø nhöõng hoa vaên ñaõ thu ñöôïc, chuùng ta coù theå tieán haønh phaân loaïi nhöõng hoa vaên naøy thaønh caùc lôùp hoa vaên nhö sau: - Lôùp hoa vaên chuyeân phuïc vuï cho vieäc trang trí trong laõnh vöïc xaây döïng vaø kieán truùc - Lôùp hoa vaên chuyeân phuïc vuï cho vieäc thieát keá myõ thuaät, trang phuïc, thôøi trang - Lôùp hoa vaên khaùc Caùc Lôùp Hoa Vaên Lôùp Hoa Vaên Phuïc Vuï Cho Vieäc Trang Trí Trong Laõnh Vöïc Xaây Döïng Vaø Kieán Truùc Ñaây laø lôùp hoa vaên ñôn giaûn thöôøng ñöôïc söû duïng trong caùc coâng trình xaây döïng, trang trí noäi thaát. Lôùp hoa vaên naøy bao goàm nhöõng hoa vaên laø nhöõng hình chöõ nhaät ñöôïc khaûm baèng nhöõng khoái hình hoïc mang tính ñoái xöùng ví duï nhö: hình vuoâng, hình nguõ giaùc, hình luïc giaùc, hình troøn, v..v. Trong laõnh vöïc xaây döïng, nhöõng hoa vaên noùi treân thöôøng ñöôïc söû duïng trong caùc coâng trình xaây döïng ñôn giaûn nhö loùt neàn nhaø, lôïp maùi,v..v Lôùp Hoa Vaên Phuïc Vuï Cho Vieäc Thieát Keá Myõ Thuaät, Trang Phuïc, Thôøi Trang Vôùi tính myõ thuaät cao hôn so vôùi lôùp hoa vaên noùi treân, lôùp hoa vaên naøy – bao goàm nhöõng hoa vaên nhö cung roàng, cung C, caùc hình aûnh khoâng mang tính ñoái xöùng. Lôùp hoa vaên naøy thöôøng ñöôïc öùng duïng ñeå phaùt trieån caùc maãu maõ trong laõnh vöïc thieát keá thôøi trang, caùc öùng duïng veà myõ thuaät. Lôùp Hoa Vaên Khaùc Lôùp hoa vaên naøy laø nhöõng hình aûnh raát phoå bieán trong thöïc teá, maëc duø khoâng mang tính myõ thuaät nhieàu nhöng thöïc teá cuõng hay thöôøng ñöôïc söû duïng nhö hình ngoâi sao naêm caùnh, hình aâm döông, v..v Toùm Taét Ngaøy nay, trong haàu heát caùc goùi phaàn meàm öùng duïng trong ñoà hoïa, caùc ñoái töôïng cô sôû noùi treân ñeàu ñöôïc taïo ra töø nhöõng thö vieän chuyeân duïng, nhö vaäy vieäc thieát keá caùc heä Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 77 patterns ñôn giaûn laø phaûi ñöa ra ñöôïc moät giaûi thuaät hieäu quaû ñeå taïo ra moät hieäu öùng myõ thuaät höõu hieäu. Trong quaù trình thieát keá moät heä patterns, chuùng ta cuõng neân phaân bieät töøng loaïi caùc patterns khaùc nhau toå chöùc moät caùch hôïp lyù nhaèm cho ngöôøi söû duïng sau naøy coù theå deã daøng tham khaûo ñeán. Ví duï nhö, coù heä patterns chuyeân phuïc vuï cho thieát keá thôøi trang (ñoù laø caùc heä caro, caùc hình troøn saép xeáp theo traät töï nhaát ñònh, caùc cung roàng, cung C thieát keá moät caùch uyeån chyeån vaø trang nhaõ,…) hay caùc heä pattern phuïc vuï cho caùc coâng trình kieán truùc nhö heä hoa vaên trang trí cho töôøng nhaø, neàn nhaø, v..v.. Taát caû caùc heä patterns noùi treân seõ laàn löôït ñöôïc hieän thöïc trong chöông trình taïo heä patterns cuûa ñoà aùn naøy. Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 78 Chöông 6 NGOÂN NGÖÕ VISUAL C++ Keå töø khi môùi ra ñôøi cho ñeán ngaøy nay laø version 6.0, Visual C++ - hay coøn coù moät teân goïi khaùc laø Microsoft Developer Studio - ñaõ chöùng toû ñöôïc raèng noù moät trong nhöõng ngoân ngöõ laäp trình raát höõu hieäu ñeå xaây döïng caùc öùng duïng 32 bit chaïy trong moâi tröôøng Windows 95 vaø Windows NT. Nhö chuùng ta ñaõ bieát, caùc chöông trình öùng duïng ngaøy nay - döôùi moät goùc nhìn naøo ñoù laø caùc chöông trình öùng duïng coù quy moâ lôùn hôn vaø ñoä phöùc taïp hôn so vôùi tröôùc ñaây, hôn nöõa, beân caïnh quy moâ lôùn vaø phöùc taïp thì giao dieän ñoà hoïa laø khoâng theå thieáu trong nhöõng chöông trình öùng duïng naøy. Tuy nhieân, cho duø caùc chöông trình öùng duïng ngaøy caøng ñoøi hoûi tính quy moâ hôn, phöùc taïp hôn thì ngöôøi laäp trình vaãn coù khaû naêng giaûm thieåu caùc coâng vieäc cuûa mình neáu hoï choïn ñöôïc moät coâng cuï laäp trình hôïp lyù. Visual C++ laø moät trong nhöõng coâng cuï noùi treân. Vôùi code-generating wizards, Visual C++ coù theå taïo ra ñöôïc phaàn coát loõi cho caùc chöông trình öùng duïng thöïc thi ñöôïc döôùi moâi tröôøng Windows trong voøng vaøi giaây ñoàng hoà. Caùc lôùp thö vieän ñi keøm vôùi Visual C++ nhö laø Microsoft Foundation Classes ngaøy nay ñaõ trôû thaønh moät trong caùc tieâu chuaån ñeå phaùt trieån caùc phaàn meàm döôùi moâi tröôøng Windows cho caùc trình bieân dòch C khaùc nhau. Beân caïnh ñoù, caùc coâng cuï soaïn thaûo khaû kieán cuõng taïo ra cho chuùng ta caùc coâng cuï höõu hieäu ñeå coù theå taïo caùc menu vaø caùc hoäp hoäi thoaïi. Noùi toùm laïi, vieäc nghieân cöùu Visual C++ laø moät vieäc laøm caàn thieát cho caùc nhaø laäp trình öùng duïng ñoøi hoûi caùc chöông trình thöïc thi döôùi moâi tröôøng Windows vaø giao dieän ñoà hoïa. Ñeå cung caáp cho chuùng ta – nhöõng ngöôøi laäp trình – moät moâi tröôøng laøm vieäc hieäu quaû, Microsoft Developer Studio ñaõ keát hôïp caùc giai ñoaïn cuûa vieäc laäp trình nhö laø soaïn thaûo, bieân dòch vaø kieåm tra loãi chung laïi thaønh moät giao dieän duy nhaát, ñieàu ñoù coù nghóa chuùng ta coù theå caøi ñaët hay xoaù boû caùc breakpoints phuïc vuï cho vieäc debug chöông trình trong cuøng moät giao dieän vôùi vieäc soaïn thaûo phaàn source code. Hôn theá nöõa, khi chuùng ta tieán haønh xaây döïng moät ñeà aùn, IDE (Integrated Development Environment) coù theå phaùt hieän caùc loãi vaø cho pheùp chuùng ta nhaûy ngay ñeán vò trí loãi ñoù moät caùch nhanh choùng cho duø taäp tin chöùa caùc loãi naøy khoâng ñöôïc môû. Project Workspace Thoâng thöôøng thì moät trong nhöõng ñieåm phöùc taïp nhaát vaø khoù kieåm soaùt nhaát khi vieát caùc chöông trình öùng duïng döôùi moâi tröôøng Windows laø vieäc kieåm soaùt caùc tieán trình. Tuy nhieân vôùi Visual C++, caùc tieán trình coù theå ñöôïc kieåm soaùt moät caùch töï ñoäng baèng Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 79 Developer Studio. Baát kyø moät thay ñoåi naøo xaûy ra trong ñeà aùn cuûa baïn, Developer Studio cuõng ñeàu töï ñoäng ghi laïi trong caùc taäp tin moâ taû ñeà aùn maø khoâng caàn baát keå moät söï can thieäp naøo töø phiaù ngöôøi laäp trình. Trong Visual C++ 5.0, caùc thoâng tin noùi treân ñöôïc löu tröõ trong project (.dsp) files. Tuy nhieân, caùc thoâng tin ñaùng löu yù ñeàu ñöôïc Visual C++ löu tröõ trong caùc files project workspace (.dsw). Caùc files naøy coù traùch nhieäm löu tröõ toaøn boä caùc thoâng tin ñöôïc söû duïng nhaèm lieân keát taát caû caùc files taïo neân ñeà aùn, keå caû caùc files source code vaø project files. Caùc files naøy cuõng löu tröõ luoân caû nhöõng caøi ñaët cuûa Developer Studio. Vôùi Visual C++, chöông trình öùng duïng thöôøng ñöôïc xaây döïng treân cô sôû caùc lôùp (classes). Coù raát nhieàu lôùp khaùc nhau ñaõ ñöôïc xaây döïng saün thaønh thö vieän ñeå phuïc vuï cho ngöôøi laäp trình. Tuy nhieân trong phaïm vi haïn heïp cuaû ñoà aùn thöïc taäp toát nghieäp naøy, chuùng ta cuõng khoâng coù tham voïng ñi trình baøy chi tieát töøng lôùp cuï theå maø chæ troïng taâm chuù yù ñeán lôùp CDC (CDC Class) laø lôùp phuïc vuï raát ñaéc löïc cho vieäc veõ hình trong caùc öùng duïng vieát baèng Visual C++. CDC Class Vaø Phaïm Vi Thieát Bò Moät caùch chuyeân duïng, CDC Class ñöôïc thieát keá cho muïc ñích chính laø veõ hình khi chuùng ta caàn laøm vieäc trong caùc windows ñeå taïo nhöõng hình aûnh thoâng baùo hay ñeå thöïc thi haøm OnDraw(). Trong thöïc teá thì CDC Class ñöôïc thieát keá bao goàm hai phaïm vi thieát bò: moät duøng cho vieäc quaûn lyù caùc quaù trình veõ vaø caùi coøn laïi duøng cho vieäc quaûn lyù caùc quaù trình hoûi ñaùp – chaát vaán (query). Vieäc quaûn lyù quaù trình veõ treân caùc phaïm vi thieát bò ñöôïc thöïc thi bôûi m_hDC vaø vieäc quaûn lyù caùc quaù trình chaát vaán laø do m_hAttribDC. Khi chuùng ta can thieäp vaøo CDC duø vôùi moät coâng vieäc sô ñaúng nhaát thì ñaùp öùng töông töï seõ xaûy ra cho phaïm vi thieát bò töông öùng, chuùng ta khoâng caàn phaûi lo sôï gì khi söû dung CDC vaø chuùng ta cuõng coù theå thöïc hieän caùc coâng vieäc cuûa chuùng ta moät caùch ñôn giaûn laø söû duïng caùc haøm thaønh phaàn cuûa CDC. Caùc Haøm Trong CDC Class Caùc haøm ñöôïc ñònh nghóa trong CDC cho pheùp chuùng ta thöïc hieän vieäc veõ hình hay chæ roõ caùch maø caùc quaù trình veõ seõ ñöôïc thöïc thi. Haàu heát caùc haøm trong CDC laø ñeàu phuïc vuï cho coâng vieäc thöù nhaát. Coù khoaûng vaøi chuïc haøm ñeå thöïc hieän vieäc veõ naøy. Moät khaùi nieäm chung coù theå ñöa ra ñaây laø khaùi nieäm veà ñieåm hieän haønh. Ñieåm hieän haønh cuûa phaïm vi thieát bò laø ñieåm maø nôi ñoù quaù trình veõ seõ ñöôïc tieán haønh neáu nhö chuùng ta khoâng chæ roõ ñieåm naøo khaùc. Haøm MoveTo() seõ dòch chuyeån ñieåm hieän haønh treân phaïm vi thieát bò maø khoâng thöïc hieän quaù trình veõ naøo caû. Ngoaøi ra chuùng ta cuõng coù theå söû duïng haøm LineTo() ñeå veõ moät ñöôøng thaúng töø ñieåm hieän haønh ñeán ñieåm maø chuùng ta chæ roõ trong tham soá cuûa haøm LineTo(). Nhö vaäy, chuùng ta coù theå tieán haønh veõ moät ñöôøng thaúng töø vò trí (x1, y1) ñeán vò trí (x2, y2) baèng hai leänh nhö sau: pDC->MoveTo(x1, y1); pDC->LineTo(x2, y2); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 80 Caùc haøm thaønh phaàn khaùc cuûa CDC duøng ñeå veõ caùc hình daùng töông ñoái phöùc taïp hôn. Chuùng ta coù theå veõ hình chöõ nhaät baèng caùch goïi haøm Rectangle() hay veõ ellipse baèng caùch goïi haøm Ellipse() vaø cuõng töông töï cho caùc haøm khaùc. Neáu nhö caùc haøm treân laø khoâng ñuû söû duïng thì CDC coøn cho pheùp chuùng ta söû duïng haøm Polygon() ñeå veõ caùc ña giaùc chính quy hay khoâng chính quy baèng vieäc cung caáp cho haøm caùc ñieåm töông öùng. Vieäc Söû Duïng Caùc Heä Toïa Ñoä Moät trong nhöõng ñieàu quan troïng nhaát cuûa caùc haøm lieân quan ñeán cheá ñoä veõ laø heä toïa ñoä, noù seõ ñöôïc duøng ñeå tieán haønh veõ trong phaïm vi thieát bò. Heä truïc toïa ñoä söû duïng ñöôïc chæ ra trong taäp caùc mapping mode. Ñieàu naøy seõ laøm cho Windows bieát ñöôïc laøm theá naøo ñeå aùnh xaï caùc toïa ñoä maø chuùng ta chæ ra trong caùc haøm duøng ñeå veõ töông öùng ñeán caùc pixel maø phaïm vi thieát bò coù theå quaûn lyù. Cheá ñoä veõ MM_TEXT laø moät trong caùc cheá ñoä thöôøng ñöôïc söû duïng nhaát. Khi söû duïng cheá ñoä naøy thì caùc toïa ñoä chuùng ta chæ ra trong caùc haøm thöïc hieän vieäc veõ seõ ñöôïc aùnh xaï tröïc tieáp leân ñôn vò hieån thò ñôn treân thieát bò xuaát cuûa chuùng ta. Ví duï nhö ñoái vôùi maùy in thì noù chæ ra raèng moät ñôn vò cuûa maät ñoä in seõ ñöôïc aùnh xaï ñeán. Ñoái vôùi thieát bò hieån thò thì moät ñôn vò toïa ñoä seõ töông öùng vôùi moät pixel Ngoaøi ra trong lôùp CDC chuùng ta coøn coù nhieàu lôùp con khaùc nhö laø CFont phuïc vuï cho vieäc quaûn lyù font chöõ, CPen phuïc vuï cho vieäc choïn daùng kieåu cho ñöôøng thaúng ñöôïc in ra, CBitmap laø daïng maø Windows coù theå hieån thò hình aûnh trong baát kyø moät windows naøo. CBrush duøng ñeå chæ ra caùch thöùc toâ maøu moät ñoái töôïng ñöôïc veõ. CPalette duøng ñeå ñònh nghóa caùc baûng maøu. CRgn ñöôïc söû duïng khi chuùng ta muoán windows tieán haønh veõ trong moät vuøng ñònh tröôùc maø khoâng laáp caùc vuøng khaùc. Baûng döôùi ñaây lieät keâ cho chuùng ta moät soá haøm coù maët trong CDC Class Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 81 Baûng lieät keâ caùc haøm trong CDC Class AbortDoc Arc Attach BitBlt CDC Chord CreateCompatibleDC CreateDC CreateIC DeleteDC DeleteTempMap Detach DPtoLP DrawFocusRect DrawIcon DrawText Ellipse EndDoc EndPage EnumObjects Escape ExcludeClipRect ExcludeUpdateRgn ExtFloodFill ExtTextOut FillRect FillRgn FloodFill FrameRect FrameRgn GetAspectRatioFilter GetBkColor GetBkMode GetBoundsRect GetBrushOrg GetCharABCWidths GetCharWidth GetClipBox GetCurrentPosition GetMapMode GetNearestColor GetOutlineTextMetrics GetOutputCharWidth GetOutputTabbedTextExtent GetOutputTextExtent GetOutputTextMetrics GetPixel GetPolyFillMode GetROP2 GetSafeHdc GetStretchBltMode GetTabbedTextExtent GetTextAlign GetTextCharacterExtra GetTextColor GetTextExtent GetTextFace GetTextMetrics GetViewportExt GetViewportOrg GetWindowExt GetWindowOrg GrayString IntersectClipRect InvertRect InvertRgn IsPrinting LineTo LPtoDP MoveTo OffsetClipRgn OffsetViewport OffsetWindowOrg PaintRgn PatBlt Pie PlayMetaFile Polygon RealizePalette Rectangle RectVisible ReleaseAttribDC ReleaseOutputDC ResetDC RestoreDC RoundRect SaveDC ScaleViewportExt ScaleWindowExt ScrollDC SelectClipRgn SelectObject SelectPalette SelectStockObject SetAbortProc SetAttribDC SetBkColor SetBkMode SetBoundsRect SetBrushOrg SetMapMode SetMapperFlags SetOutputDC SetPixel SetPolyFillMode SetROP2 SetStretchBltMode SetTextAlign SetTextCharacterExtra SetTextColor OrgSetTextJustification SetViewportExt SetViewportorg SetWindowExt SetWindowOrg StartDoc StartPage Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 82 GetDeviceCaps GetFontData GetGlyphOutline GetKerningPairs Polyline PolyPolygon PtVisible QueryAbort StretchBlt TabbedTextOut TextOut UpdateColors Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 83 CHÖÔNG TRÌNH NGUOÀN Do heä chöông trình öùng duïng bao goàm raát nhieàu files khaùc nhau (chöông trình ñöôïc vieát baèng ngoân ngöõ Visual C++) neân ñoà aùn naøy khoâng coù tham voïng trình baøy taát caû caùc files. ÔÛ ñaây, ñoà aùn chæ in ra nhöõng files chuû yeáu ñeå ngöôøi ñoïc tieân tham khaûo patternsView.cpp // patternsView.cpp : implementation of the CPatternsView class // #include "ndc.h" #include "stdafx.h" #include "patterns.h" #include #include "patternsDoc.h" #include "patternsView.h" #include "math.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPatternsView IMPLEMENT_DYNCREATE(CPatternsView, CView) BEGIN_MESSAGE_MAP(CPatternsView, CView) //{{AFX_MSG_MAP(CPatternsView) ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPatternsView construction/destruction CPatternsView::CPatternsView() { Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 84 // TODO: add construction code here m_Display = eN_Turtlel_House; } CPatternsView::~CPatternsView() { } BOOL CPatternsView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CPatternsView drawing void CPatternsView::OnDraw(CDC* pDC) { CPatternsDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CRect rect; unsigned int bankinh; unsigned int sodinh; unsigned int solanLap; int xCenter, yCenter; PointType diemDau, diemCuoi, diembd; double gocbd; PointType tamdiem; TPolygon ndagiac; double a, b; GetClientRect(rect); xCenter = rect.right / 2; yCenter = rect.bottom / 2; tamdiem.x = xCenter; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 85 tamdiem.y = yCenter; // TODO: add draw code for native data here switch (m_Display) { case eRosettes: bankinh=150; sodinh=21; Rosettes(pDC, bankinh, tamdiem, sodinh); Circle(pDC, tamdiem, bankinh*1.5); sodinh=16; MakeNGon(sodinh,1.4*bankinh,0,tamdiem,&ndagiac); //Draw_Polygon(pDC,ndagiac); for (solanLap=1;solanLap<=sodinh;solanLap++) { Rosettes(pDC, bankinh/3, ndagiac.Points[solanLap], sodinh); Circle(pDC, ndagiac.Points[solanLap], 1.2*bankinh/3); } break; case eSixgon: //Six_Gon(pDC,tamdiem,0.5,60,1.5,150); Star(pDC,tamdiem,0.5,216,0.5,360); //Star(pDC,tamdiem,5,89.5,0.5,500); break; case eFive_Bugs_Tile: bankinh=150; sodinh=21; gocbd=0; FiveBugsTile(pDC, bankinh, tamdiem, gocbd); MakeNGon(5,bankinh*1.2,0,tamdiem, &ndagiac); Draw_Polygon(pDC, ndagiac); break; case eCCurves: solanLap = 10; SetPoint(xCenter+155,yCenter-80,&diemDau); SetPoint(xCenter+155,yCenter+80,&diemCuoi); CCurves(pDC, diemCuoi, diemDau, solanLap); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 86 SetPoint(xCenter-155,yCenter+80,&diemDau); SetPoint(xCenter-155,yCenter-80,&diemCuoi); CCurves(pDC, diemCuoi, diemDau, solanLap); break; case eDragonCurves: solanLap = 10; SetPoint(xCenter+150,yCenter-80,&diemDau); SetPoint(xCenter+150,yCenter+120,&diemCuoi); DragonCurves(pDC, diemDau, diemCuoi, solanLap); break; case eN_Turtlel_House: a = 0.5; b = 0.5; solanLap = 2000; SetPoint(xCenter,yCenter,&diembd); // N_Turtlel_House(pDC, solanLap, a, b, diembd); gocbd=0; SetPoint(xCenter, yCenter,&tamdiem); // while (gocbd!=360) // { //Sphinx(pDC,tamdiem,90,gocbd,1); DrawSphinx0(pDC,tamdiem,90,false); // gocbd+=60; // } break; case eNestBee: NestBee(pDC, tamdiem); break; case eAm_Duong: bankinh=150; AmDuong(pDC, tamdiem, bankinh); MakeNGon(6,1.4*bankinh,0,tamdiem,&ndagiac); for (solanLap=1;solanLap<=5;solanLap++) { MakeNGon(240,bankinh+10*solanLap,0,tamdiem,&ndagiac); Draw_Polygon(pDC,ndagiac); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 87 } break; case eCairo: Cairo(pDC, tamdiem); break; } } ///////////////////////////////////////////////////////////////////////////// // CPatternsView diagnostics #ifdef _DEBUG void CPatternsView::AssertValid() const { CView::AssertValid(); } void CPatternsView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CPatternsDoc* CPatternsView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPatternsDoc))); return (CPatternsDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CPatternsView message handlers void CPatternsView::Rosettes(CDC * pDC, double bkinh, PointType Center, unsigned int sodinh) { double gocbd; TPolygon N_gon; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 88 int i,j; gocbd=0; MakeNGon(sodinh,bkinh,gocbd,Center,&N_gon); for (i=1;i<=N_gon.Num-1;i++) { for (j=i+1;j<=N_gon.Num;j++) { LineReal(pDC, N_gon.Points[i],N_gon.Points[j]); } } } void CPatternsView::CCurves(CDC * pDC, PointType diemDau, PointType diemCuoi, unsigned int solanLap) { if (solanLap > 0) { PointType diemGiua; double cdai, gocbd; cdai = sqrt(pow((diemDau.x - diemCuoi.x), 2) + pow((diemDau.y - diemCuoi.y), 2)); if (cdai <= 2) { MoveToReal(pDC,diemDau); LineToReal(pDC,diemCuoi); } else { if (diemDau.y > diemCuoi.y) gocbd = acos((diemDau.x - diemCuoi.x)/cdai); else gocbd = -acos((diemDau.x - diemCuoi.x)/cdai); diemGiua.x = (short)(diemDau.x - FCT * cdai * cos(gocbd - PI/4)); diemGiua.y = (short)(diemDau.y - FCT * cdai * sin(gocbd - PI/4)); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 89 CCurves(pDC, diemDau, diemGiua, solanLap - 1); CCurves(pDC, diemGiua, diemCuoi, solanLap - 1); } } else { MoveToReal(pDC,diemDau); LineToReal(pDC,diemCuoi); } } void CPatternsView::DragonCurves(CDC* pDC, PointType diemDau, PointType diemCuoi, unsigned int solanLap) { if (solanLap > 0) { PointType diemGiua; double cdai, gocbd; cdai = sqrt(pow((diemDau.x - diemCuoi.x), 2) + pow((diemDau.y - diemCuoi.y), 2)); if (cdai <= 2) { MoveToReal(pDC,diemDau); LineToReal(pDC,diemCuoi); } else { if (diemDau.y > diemCuoi.y) gocbd = acos((diemDau.x - diemCuoi.x)/cdai); else gocbd = -acos((diemDau.x - diemCuoi.x)/cdai); diemGiua.x = (short)(diemDau.x - FCT * cdai * cos(gocbd + PI/4)); diemGiua.y = (short)(diemDau.y - FCT * cdai * sin(gocbd + PI/4)); DragonCurves(pDC, diemDau, diemGiua, solanLap - 1); DragonCurves(pDC, diemCuoi, diemGiua, solanLap - 1); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 90 } } else { MoveToReal(pDC,diemDau); LineToReal(pDC,diemCuoi); } } void CPatternsView::N_Turtlel_House(CDC* pDC, unsigned int solanLap, double d1, double d2, PointType diembd) { PointType diemtg; double angle=0; unsigned int i; diemtg.x = (short)(diembd.x+d1*cos(angle)); diemtg.y = (short)(diembd.y+d2*sin(angle)); MoveToReal(pDC,diemtg); for (i=1;i<solanLap;i++) { LineToReal(pDC,diemtg); diemtg.x = (short)(diembd.x+d1*cos(angle)); diemtg.y = (short)(diembd.y+d2*sin(angle)); d1+=0.1; d2+=0.05; angle+=PI/36; } } void CPatternsView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if (m_Display == eRosettes) m_Display = eCCurves; else if (m_Display == eCCurves) m_Display = eDragonCurves; else if (m_Display == eDragonCurves) m_Display = eN_Turtlel_House; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 91 else if (m_Display == eN_Turtlel_House) m_Display = eFive_Bugs_Tile; else if (m_Display == eFive_Bugs_Tile) m_Display = eAm_Duong; else if (m_Display == eAm_Duong) m_Display = eSixgon; else if (m_Display == eSixgon) m_Display = eCairo; else if (m_Display == eCairo) m_Display = eNestBee; else m_Display = eRosettes; Invalidate(); CView::OnLButtonDown(nFlags, point); } void CPatternsView::MakeNGon(int n, double Radius, double g0, PointType Center, TPolygon *N_gon) { int i; double g; if ((n>2) && (n<=MaxPoints)) { g=2*PI/n; // goc se quay cho moi dinh g0=g0*2*PI/360; // doi ra radian (*N_gon).Num=n; for (i=1;i<=(*N_gon).Num;i++) // tinh toa do tung dinh { (*N_gon).Points[i].x=Center.x+Radius*cos(g0); (*N_gon).Points[i].y=Center.y+Radius*sin(g0); g0=g0+g; } } } void CPatternsView::Draw_Polygon(CDC* pDC,TPolygon plg) { Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 92 int i; pDC->MoveTo((int)(plg.Points[1].x),(int)(plg.Points[1].y)); for (i=2;i<=plg.Num;i++) { pDC->LineTo((int)(plg.Points[i].x),(int)(plg.Points[i].y)); } pDC->FloodFill((int)(plg.Points[1].x),(int)(plg.Points[1].y),0x0111111A); pDC->LineTo((int)(plg.Points[1].x),(int)(plg.Points[1].y)); } void CPatternsView::LineReal(CDC* pDC, PointType p1, PointType p2) { MoveToReal(pDC, p1); LineToReal(pDC, p2); } void CPatternsView::MoveToReal(CDC* pDC, PointType p) { pDC->MoveTo((int)(p.x), (int)(p.y)); } void CPatternsView::LineToReal(CDC* pDC, PointType p) { pDC->LineTo((int)(p.x), (int)(p.y)); } void CPatternsView::FiveBugsTile(CDC* pDC, double R, PointType Center, double g) { PointType A,B,C,D,E; PointType At,Bt,Ct,Dt,Et; //Cac diem temp de tinh toan PointType A1,B1,C1,D1,E1; int i; //So lan lap Interation Counter double f; int n; TIntersect CutKind; TPolygon Polygon; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 93 f=0.5; n=50; if (f>0 && f<=1) { MakeNGon(5,R,g,Center,&Polygon); A=Polygon.Points[1]; B=Polygon.Points[2]; C=Polygon.Points[3]; D=Polygon.Points[4]; E=Polygon.Points[5]; for (i=1;i<=n;i++) { Draw_Polygon(pDC, Polygon); NewPoint(C,B,&B1); NewPoint(D,C,&C1); NewPoint(E,D,&D1); NewPoint(A,E,&E1); NewPoint(B,A,&A1); At=A;Bt=B;Ct=C;Dt=D;Et=E; //Tinh toan A,B,C,D moi InterSect(At,B1,Bt,C1,&A,&CutKind); InterSect(Bt,C1,Ct,D1,&B,&CutKind); InterSect(Ct,D1,Dt,E1,&C,&CutKind); InterSect(Dt,E1,Et,A1,&D,&CutKind); InterSect(Et,A1,At,B1,&E,&CutKind); Polygon.Points[1]=A; Polygon.Points[2]=B; Polygon.Points[3]=C; Polygon.Points[4]=D; Polygon.Points[5]=E; } } } Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 94 double CPatternsView::GetAngle(PointType p1, PointType p2) { double a; if (p2.x!=p1.x) { a=atan((p2.y-p1.y)/(p2.x-p1.x)); } else { a=PI/2; } if (p2.x<p1.x) { a+=PI; } if (ap1.x) { a+=2*PI; } if (a==PI/2 && p2.y<p1.y) { a+=PI; } return a; } int CPatternsView::LineOn(PointType C, PointType A, PointType B) { if ((B.x-A.x)*(C.y-A.y)==(B.y-A.y)*(C.x-A.x)) { return 1; } else { return 0; } } Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 95 void CPatternsView::InterSect(PointType A, PointType B, PointType C, PointType D, PointType *CutP, TIntersect *CutKind) { double DD,t0,u0,tc,td; *CutKind=none; //Gia su khong cat nhau // Tinh he so DD chi thi tinh chat giao nhau cua hai doan thang AB,CD DD=(B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x); if (DD!=0) //AB, CD khong song song { // Tinh thong so tai diem cat t0=((C.x-A.x)*(D.y-C.y)-(C.y-A.y)*(D.x-C.x))/DD; if (D.x!=C.x) u0=(A.x+(B.x-A.x)*t0-C.x)/(D.x-C.x); else u0=(A.y+(B.y-A.y)*t0-C.y)/(D.y-C.y); //Xet diem cat co nam trong gioi han cua hai doan thang hay khong if (t0>=0 && t0=0 && u0<=1) { (*CutP).x=A.x+(B.x-A.x)*t0; (*CutP).y=A.y+(B.y-A.y)*t0; (*CutKind)=one; } } else // DD=0, 2 doan thang song song hoac co doan trung nhau if (LineOn(C,A,B)) // C theo phuong AB { tc=(C.x-A.x)/(B.x-A.x); td=(D.x-A.x)/(B.x-A.x); if (!((tc1 && td>1))) *CutKind=many; } } void CPatternsView::NewPoint(PointType C, PointType B, PointType *B1) { double L, alpha; double f=0.05; // Moi lan di chuyen voi do dai f*BC L=sqrt((C.y-B.y)*(C.y-B.y)+(C.x-B.x)*(C.x-B.x)); alpha=GetAngle(C,B); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 96 (*B1).x=C.x+L*(1-f)*cos(alpha); (*B1).y=C.y+L*(1-f)*sin(alpha); } void CPatternsView::AmDuong(CDC* pDC, PointType p, double Radius) { // Ve hinh luong cuc Am duong PointType CLeft; // tam trai PointType CRight; // tam phai SetPoint(p.x-Radius/2,p.y,&CLeft); SetPoint(p.x+Radius/2,p.y,&CRight); // Ve vong tron ngoai //SetBKColor(Black); //Setcolor(white); Circle(pDC,p,Radius); // Ve duong cong phan cach ArcReal(pDC,CLeft,Radius/2,180,360); ArcReal(pDC,CRight,Radius/2,0,180); //Ve vong tron nho ben phai va ben trai Circle(pDC,CLeft,Radius/12); Circle(pDC,CRight,Radius/12); // To mau FillColor(pDC,(int)(CRight.x),(int)(CRight.y),0,pDC- >GetPixel((int)(CRight.x+Radius/2),(int)(CRight.y))); // FillColor(pDC,CLeft.x+Radius/12+1,CLeft.y,0,pDC- >GetPixel(CLeft.x+Radius/12,CLeft.y)); FillColor(pDC,(int)(CLeft.x+Radius/12+1),(int)(CLeft.y),0,pDC- >GetPixel((int)(CLeft.x+Radius/12),(int)(CLeft.y))); // setcolor(red); // FloodFillReal(CLeft,Red,white); // Setcolor(red); // ArcReal(pDC,CLeft,Radius/2,180,360); // Ve lai duong phan cach de loai mau trang // ArcReal(pDC,CRight,Radius/2,0,180); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 97 // Ve 2 vong tron nho // setcolor(white); // Circle(pDC,CLeft,Radius/12); // vong ben trai // FillColor(pDC,CLeft.x,CLeft.y,0,pDC- >GetPixel(CLeft.x+Radius/12,CLeft.y)); // FloodFillReal(CLeft,Blue,white); // Setcolor(blue); // Circle(pDC,CLeft,Radius/12); // Setcolor(white); // Circle(pDC,CRight,Radius/12); // vong ben phai // FillColor(pDC,CRight.x,CRight.y,0,pDC- >GetPixel(CRight.x+Radius/12,CRight.y)); // FloodFillReal(CRight,Red,White); // Setcolor(red); // Circle(pDC,CRight,Radius/12); // Setcolor(white); // Circle(pDC,C,Radius); } void CPatternsView::Circle(CDC* pDC, PointType Center, double Radius) { TPolygon dagiac; // Chung ta co the xem duong tron la mot da giac deu co so dinh du lon (200 dinh) MakeNGon(200,Radius,0,Center,&dagiac); Draw_Polygon(pDC,dagiac); } void CPatternsView::SetPoint(double x, double y, PointType *p) { (*p).x = x; (*p).y = y; } void CPatternsView::ArcReal(CDC* pDC, PointType Center, double Radius, double StartAngle, double StopAngle) { PointType p; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 98 double temp,incr; // thay doi cac goc, tang goc if (StopAngle<StartAngle) { temp=StartAngle; StartAngle=StopAngle; StopAngle=temp; } incr=(StopAngle-StartAngle)/MaxPoints; p.x=Center.x+Radius*cos(StartAngle*2*PI/360); // diem dau tien p.y=Center.y+Radius*sin(StartAngle*2*PI/360); MoveToReal(pDC,p); while (StartAngle<StopAngle) { StartAngle=StartAngle+incr; //moi lan tang mot goc incr p.x=Center.x+Radius*cos(StartAngle*2*PI/360); p.y=Center.y+Radius*sin(StartAngle*2*PI/360); LineToReal(pDC,p); } } void CPatternsView::FillColor(CDC* pDC, int x, int y, unsigned int Color, unsigned int BoundColor) { if (pDC->GetPixel(x,y) != BoundColor) { if (pDC->GetPixel(x,y) != Color) { pDC->SetPixel(x,y,Color); } FillColor(pDC,x,y+1,Color,BoundColor); FillColor(pDC,x,y-1,Color,BoundColor); FillColor(pDC,x-1,y,Color,BoundColor); FillColor(pDC,x+1,y,Color,BoundColor); } } Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 99 void CPatternsView::Six_Gon(CDC* pDC, PointType p, double dist, double angle, double incr, int num) { RealCP=p; MoveToReal(pDC, p); PolySpiral(pDC,dist,angle,incr,num); } void CPatternsView::PolySpiral(CDC* pDC, double dist, double angle, double incr, int num) { int i; for (i=1;i<=num;i++) { LineForward(pDC, dist); dist+=incr; RightTurn(angle); } } void CPatternsView::LineForward(CDC* pDC, double a) { PointType p; double angle; angle=2*PI*CD/360; // doi degree ra radian de tinh sin cos p.x=RealCP.x+a*cos(angle); p.y=RealCP.y+a*sin(angle); LineToReal(pDC, p); RealCP=p; } void CPatternsView::RightTurn(double g) { CD = CD - g; //nguoc chieu luong giac } void CPatternsView::LeftTurn(double g) { CD = CD + g; //cung chieu luong giac } Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 100 void CPatternsView::NestBee(CDC* pDC, PointType p) { PointType Start,temp_p; TRect W; int sodinh; bool All; // Chon lua ve toan bo luc giac hoac 1/2 luc giac bool Odd; // Le va chan khac toa do tam int R=30; // double H_Offset=R*c3; // Do doi tam luc giac theo hang // double V_Offset=1.5*R; // Do doi tam luc giac theo cot double H_Offset=2*R; // Do doi tam luc giac theo hang double V_Offset=c3*R; // Do doi tam luc giac theo cot // PenColor:word; // mau ve cua he thong // Pencolor=getColor; // SetBkColor(bkcolor); SetRect(30,p.y*2-30,p.x*2-30,30,&W); // SetRect(0,p.y*2,p.x*2,0,&W); RectangleReal(pDC,W.Left,W.Top,W.Right,W.Bottom); SetPoint(W.Left,W.Bottom+R,&Start); temp_p=Start; Odd=true; sodinh=6; //for (i=1;i<=2;i++) while (temp_p.y<W.Top+R) { if (Odd) temp_p.x=W.Left; else temp_p.x=W.Left+H_Offset/2; while (temp_p.x<W.Right+R) { if (temp_p.x==Start.x) All=false; else All=true; DrawElement(pDC,temp_p,sodinh,W,All); //Star(pDC,temp_p,0.5,144,0.5,180); //FiveBugsTile(pDC,R,temp_p,0); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 101 //AmDuong(pDC,temp_p,10); //Rosettes(pDC,R,temp_p,8); //FillColor(pDC,temp_p.x,temp_p.y,crColor,pDC- >GetPixel(temp_p.x,temp_p.y- R)); temp_p.x=temp_p.x+H_Offset; } Odd=!Odd; temp_p.y=temp_p.y+V_Offset; } } void CPatternsView::DrawElement(CDC* pDC, PointType p, int Num, TRect W, bool All) { // Ve mot luc giac co tam la p TPolygon Gon; PointType p1,p2; int i,j; bool visible; double R=30; double g=270; MakeNGon(Num,R,g,p,&Gon); // Ve da giac trong co cat xen if (Num==6) { if (All) j=6; else j=3; } else j=Num; for (i=1;i<=j;i++) { p1=Gon.Points[i]; if (All && i==j) p2=Gon.Points[1]; else p2=Gon.Points[i+1]; Clip(&p1,&p2,W,&visible); if (visible) LineReal(pDC,p1,p2); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 102 } } void CPatternsView::Clip(PointType *p1, PointType *p2, TRect W, bool *visible) { Half_Space_Code c1,c2,tmp_cd; PointType tmp_point; double m; //slope, he so goc cua doan thang bool in1,in2; //Chi thi hai diem co nam trong window hay khong bool done; //Chi thi xong tac vu hay chua done=false; *visible=false; do { Encode(*p1,&c1,W); Encode(*p2,&c2,W); if (c1.L && c1.T && c1.R && c1.B) in1=true; else in1=false; if (c2.L && c2.T && c2.R && c2.B) in2=true; else in2=false; if (in1 && in2) //doan thang p1p2 nam trong window { done=true; *visible=true; } else //it nhat co mot diem nam ngoai window if ((!c1.L && !c2.L) || (!c1.R && !c2.R) || /*p1p2 nam ngoai*/ (!c1.T && !c2.T) || (!c1.B && !c2.B)) { done=true; *visible=false; } else // mot diem nam ngoai, mot diem nam trong { if (in1) //p1 nam trong, doi cho p1,p2,c1,c2 { Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 103 tmp_point=*p1;*p1=*p2;*p2=tmp_point; tmp_cd=c1;c1=c2;c2=tmp_cd; } if ((*p2).x==(*p1).x) //p1p2 la duong thang dung { if (!c1.T) (*p1).y=W.Top; //p1 o tren else if (!c1.B) (*p1).y=W.Bottom; //p1 o duoi } else //p1p2 la duong thang nghieng { m=((*p2).y-(*p1).y)/((*p2).x-(*p1).x); //tinh he so goc, slope if (!c1.L) { (*p1).y=(*p1).y+(W.Left- (*p1).x)*m; (*p1).x=W.Left; } else if (!c1.R) { (*p1).y=(*p1).y+(W.Right- (*p1).x)*m; (*p1).x=W.Right; } else if (!c1.B) { (*p1).x=(*p1).x+(W.Bottom- (*p1).y)/m; (*p1).y=W.Bottom; } else if (!c1.T) { (*p1).x=(*p1).x+(W.Top- (*p1).y)/m; (*p1).y=W.Top; } } } Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 104 } while (!done); } void CPatternsView::Encode(PointType p, Half_Space_Code *c, TRect W) { (*c).L=in_out(p.x,W.Left); (*c).R=in_out(W.Right,p.x); (*c).B=in_out(p.y,W.Bottom); (*c).T=in_out(W.Top,p.y); } bool CPatternsView::in_out(double a, double b) { if (a<b) { return false; } else { return true; } } void CPatternsView::SetRect(double Left, double Top, double Right, double Bottom, TRect *r) { (*r).Left=Left; (*r).Top=Top; (*r).Right=Right; (*r).Bottom=Bottom; } void CPatternsView::RectangleReal(CDC* pDC, double x1, double y1, double x2, double y2) { PointType A; SetPoint(x1,y1,&A); MoveToReal(pDC,A); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 105 SetPoint(x2,y1,&A); LineToReal(pDC,A); SetPoint(x2,y2,&A); LineToReal(pDC,A); SetPoint(x1,y2,&A); LineToReal(pDC,A); SetPoint(x1,y1,&A); LineToReal(pDC,A); } void CPatternsView::MapRect(TRect W, TRect V) { sx=(V.Right-V.Left)/(W.Right-W.Left); sx=(V.Top-V.Bottom)/(W.Top-W.Bottom); tx=(V.Left*W.Right-W.Left*V.Right)/(W.Right-W.Left); ty=(V.Bottom*W.Top-W.Bottom*V.Top)/(W.Top-W.Bottom); } void CPatternsView::Cairo(CDC* pDC, PointType p) { PointType Start; TRect W; double L=15; SetRect(0,p.y*2,p.x*2,0,&W); RectangleReal(pDC,W.Left,W.Top,W.Right,W.Bottom); SetPoint(W.Left,W.Bottom,&Start); HorizonTile(pDC, Start, W, L); //VerticalTile(pDC, Start, W, L); } void CPatternsView::HorizonTile(CDC* pDC, PointType Start, TRect W, double L) { bool Odd; //chan - le PointType Center; Odd=true; Center=Start; while (Center.y<W.Top+3*L/2) { if (Odd) Center.x=Start.x; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 106 else Center.x=Start.x-L*c3; while (Center.x<W.Right+L*c3) { DrawElementH(pDC, Center, W, L); Center.x=Center.x+2*L*c3; } Odd=!(Odd); Center.y=Center.y+1.5*L+L/2; } } void CPatternsView::VerticalTile(CDC* pDC, PointType Start, TRect W, double L) { bool Odd; //chan - le PointType Center; Odd=true; Center.x=Start.x; while (Center.x<W.Right-L/2) { if (Odd) Center.y=Start.y; else Center.y=Start.y-L*c3; while (Center.y<W.Top+1.5*L) { DrawElementV(pDC, Center, W, L); Center.y=Center.y+2*L*c3; } Odd=!(Odd); Center.x=Center.x+2*L; } } void CPatternsView::DrawElementH(CDC* pDC, PointType Center, TRect W, double L) { PointType p1,p2,p3,p4,p5,p6; bool visible; SetPoint(Center.x-L*c3,Center.y-L/2,&p1); SetPoint(p1.x,p1.y+L,&p2); SetPoint(Center.x,Center.y+1.5*L,&p3); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 107 SetPoint(Center.x+L*c3,p2.y,&p4); SetPoint(p4.x,p4.y-L,&p5); SetPoint(Center.x,Center.y-1.5*L,&p6); Clip(&p1,&p2,W,&visible); if (visible) LineReal(pDC,p1,p2); Clip(&p2,&p3,W,&visible); if (visible) LineReal(pDC,p2,p3); Clip(&p3,&p4,W,&visible); if (visible) LineReal(pDC,p3,p4); Clip(&p4,&p5,W,&visible); if (visible) LineReal(pDC,p4,p5); Clip(&p5,&p6,W,&visible); if (visible) LineReal(pDC,p5,p6); Clip(&p6,&p1,W,&visible); if (visible) LineReal(pDC,p6,p1); } void CPatternsView::DrawElementV(CDC* pDC, PointType Center, TRect W, double L) { PointType p1,p2,p3,p4,p5,p6; bool visible; SetPoint(Center.x+L/2,Center.y-L*c3,&p1); SetPoint(p1.x-L,p1.y,&p2); SetPoint(Center.x-1.5*L,Center.y,&p3); SetPoint(p2.x,Center.y+L*c3,&p4); SetPoint(p4.x+L,p4.y,&p5); SetPoint(Center.x+1.5*L,Center.y,&p6); Clip(&p1,&p2,W,&visible); if (visible) LineReal(pDC,p1,p2); Clip(&p2,&p3,W,&visible); if (visible) LineReal(pDC,p2,p3); Clip(&p3,&p4,W,&visible); if (visible) LineReal(pDC,p3,p4); Clip(&p4,&p5,W,&visible); if (visible) LineReal(pDC,p4,p5); Clip(&p5,&p6,W,&visible); if (visible) LineReal(pDC,p5,p6); Clip(&p6,&p1,W,&visible); if (visible) LineReal(pDC,p6,p1); } void CPatternsView::Sphinx(CDC* pDC, PointType p, double Width, double Angle, int Order) { bool Right=true; bool Left=false; bool Dir; Dir=Left; MoveToReal(pDC, p); if (Order<2) DrawSph(pDC,p,Width,Angle,Order,Dir); } Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 108 void CPatternsView::DrawSphinx0(CDC* pDC, PointType p, double W, bool Dir) { bool Left=false; if (Dir==Left) { LineForward(pDC,W); LeftTurn(120); LineForward(pDC,2*W/3); LeftTurn(120); LineForward(pDC,W/3); RightTurn(60); LineForward(pDC,W/3); LeftTurn(60); LineForward(pDC,W/3); } else { LineForward(pDC,W); RightTurn(120); LineForward(pDC,2*W/3); RightTurn(120); LineForward(pDC,W/3); LeftTurn(60); LineForward(pDC,W/3); RightTurn(60); LineForward(pDC,W/3); } } void CPatternsView::DrawSph(CDC* pDC, PointType p, double W, double a, int Order, bool Parent_Dir) { int i; PointType B,C,D; float aDegree; TArr pArr[16]; if (Order==0) { MoveToReal(pDC, p); CD=a; DrawSphinx0(pDC,p,W,Parent_Dir); } else { // Tinh toan 16 diem phan ra for (i=1;i<=16;i++) Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 109 { pArr[i].Child_w=W/4; } aDegree=(float)(a); a=a*RADS; //Doi ra radian pArr[1].Child_p=p; pArr[1].Child_a=aDegree; pArr[1].Child_dir=Parent_Dir; B.x=p.x+(W/6)*cos(a+PI/3); B.y=p.x+(W/6)*sin(a+PI/3); //B.y=p.y+(W/6)*sin(a+PI/3); C.x=p.x+(W/3)*cos(a+PI/3); C.y=p.y+(W/3)*sin(a+PI/3); D.x=p.x+W*cos(a); D.y=p.y+W*sin(a); pArr[2].Child_p.x=B.x+(4*pArr[2].Child_w/3)*cos(a); pArr[2].Child_p.y=B.y+(4*pArr[2].Child_w/3)*sin(a); pArr[2].Child_a=aDegree+180; pArr[2].Child_dir=Parent_Dir; pArr[3].Child_p.x=p.x+(W/4)*cos(a); pArr[3].Child_p.y=p.y+(W/4)*sin(a); pArr[3].Child_a=aDegree; pArr[3].Child_dir=Parent_Dir; pArr[5].Child_p.x=p.x+(W/2)*cos(a); pArr[5].Child_p.y=p.y+(W/2)*sin(a); pArr[5].Child_a=aDegree; pArr[5].Child_dir=Parent_Dir; pArr[4].Child_p.x=pArr[5].Child_p.x+pArr[4].Child_w*cos(a+PI/3); pArr[4].Child_p.y=pArr[5].Child_p.y+pArr[4].Child_w*sin(a+PI/3); pArr[4].Child_a=aDegree+60+180; pArr[4].Child_dir= !(Parent_Dir); pArr[6].Child_p.x=B.x+(3*pArr[6].Child_w+pArr[6].Child_w/3)*cos(a); pArr[6].Child_p.y=B.y+(3*pArr[6].Child_w+pArr[6].Child_w/3)*sin(a); pArr[6].Child_a=aDegree+180; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 110 pArr[6].Child_dir=Parent_Dir; pArr[7].Child_p.x=p.x+(3*W/4)*cos(a); pArr[7].Child_p.y=p.y+(3*W/4)*sin(a); pArr[7].Child_a=aDegree; pArr[7].Child_dir=Parent_Dir; pArr[8].Child_p.x=D.x+(5*pArr[8].Child_w/3)*cos(a+120*RADS); pArr[8].Child_p.y=D.y+(5*pArr[8].Child_w/3)*sin(a+120*RADS); pArr[8].Child_a=aDegree+120+180; pArr[8].Child_dir=!(Parent_Dir); pArr[9].Child_p.x=pArr[6].Child_p.x+(2*pArr[9].Child_w/3)*cos(a+PI); pArr[9].Child_p.y=pArr[6].Child_p.y+(2*pArr[9].Child_w/3)*sin(a+PI); pArr[9].Child_a=aDegree+120; pArr[9].Child_dir=!(Parent_Dir); pArr[10].Child_p.x=pArr[8].Child_p.x+pArr[10].Child_w*cos(a+120*RADS); pArr[10].Child_p.y=pArr[8].Child_p.y+pArr[10].Child_w*sin(a+120*RADS); pArr[10].Child_a=aDegree+120+180; pArr[10].Child_dir=!(Parent_Dir); pArr[11].Child_a=aDegree+120+120; pArr[11].Child_p.x=pArr[10].Child_p.x+(pArr[11].Child_w/3)*cos(pArr[11].Child_a*RADS) ; pArr[11].Child_p.y=pArr[10].Child_p.y+(pArr[11].Child_w/3)*sin(pArr[11].Child_a*RADS); pArr[11].Child_dir=Parent_Dir; pArr[12].Child_p.x=pArr[5].Child_p.x+(pArr[12].Child_w/3)*cos(a+PI/3); pArr[12].Child_p.y=pArr[5].Child_p.y+(pArr[12].Child_w/3)*sin(a+PI/3); pArr[12].Child_a=aDegree+60; pArr[12].Child_dir=!(Parent_Dir); pArr[13].Child_p.x=C.x+2*pArr[13].Child_w*cos(a); pArr[13].Child_p.y=C.y+2*pArr[13].Child_w*sin(a); pArr[13].Child_a=aDegree+180; pArr[13].Child_dir=Parent_Dir; pArr[14].Child_p.x=B.x+(2*pArr[14].Child_w/3)*cos(a); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 111 pArr[14].Child_p.y=B.y+(2*pArr[14].Child_w/3)*sin(a); pArr[14].Child_a=aDegree; pArr[14].Child_dir=Parent_Dir; pArr[15].Child_p.x=C.x+pArr[15].Child_w*cos(a);; pArr[15].Child_p.y=C.y+pArr[15].Child_w*sin(a);; pArr[15].Child_a=aDegree+180; pArr[15].Child_dir=Parent_Dir; pArr[16].Child_p.x=p.x+(pArr[16].Child_w/3)*cos(a+PI/3); pArr[16].Child_p.y=p.y+(pArr[16].Child_w/3)*sin(a+PI/3); pArr[16].Child_a=aDegree+60; pArr[16].Child_dir=!(Parent_Dir); for (i=1;i<=16;i++) { DrawSph(pDC,pArr[i].Child_p,pArr[i].Child_w,pArr[i].Child_a,Order- 1,pArr[i].Child_dir); } } } void CPatternsView::Star(CDC* pDC, PointType p, double dist, double angle, double incr, int num) { RealCP=p; MoveToReal(pDC, p); PolySpiral(pDC,dist,angle,incr,num); } patternsView.h // patternsView.h : interface of the CPatternsView class // ///////////////////////////////////////////////////////////////////////////// #include "ndc.h" #if !defined(AFX_PATTERNSVIEW_H__0AD4B2CD_E2E7_11D2_9C9A_444553540000__IN CLUDED_) Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 112 #define AFX_PATTERNSVIEW_H__0AD4B2CD_E2E7_11D2_9C9A_444553540000__INCLUDED _ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #define FCT 0.7071067811865 #define PI 3.14159265395 #define c3 1.7320508076 // Can bac hai cua 3 : sqrt(3) class CPatternsView : public CView { protected: // create from serialization only CPatternsView(); DECLARE_DYNCREATE(CPatternsView) // Attributes protected: enum {eRosettes, eCCurves, eDragonCurves, eN_Turtlel_House, eFive_Bugs_Tile, eAm_Duong, eSixgon, eNestBee, eCairo} m_Display; double CD; PointType RealCP; TRect NDCViewport; double sx; double sy; double tx; double ty; public: CPatternsDoc* GetDocument(); // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CPatternsView) Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 113 public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: //}}AFX_VIRTUAL // Implementation public: virtual ~CPatternsView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: void Star(CDC* pDC, PointType p, double dist, double angle, double incr, int num); void DrawSph(CDC* pDC, PointType p, double W, double a, int Order, bool Parent_Dir); void DrawSphinx0(CDC* pDC, PointType p, double W, bool Dir); void Sphinx(CDC* pDC, PointType p, double Width, double Angle, int Order); void DrawElementV(CDC* pDC, PointType Center, TRect W, double L); void DrawElementH(CDC* pDC, PointType Center, TRect W, double L); void VerticalTile(CDC* pDC, PointType Start, TRect W, double L); void HorizonTile(CDC* pDC, PointType Start, TRect W, double L); void Cairo(CDC* pDC, PointType p); void MapRect(TRect W, TRect V); void RectangleReal(CDC* pDC, double x1, double y1, double x2, double y2); void SetRect(double Left, double Top, double Right, double Bottom, TRect *r); bool in_out(double a, double b); void Encode(PointType p, Half_Space_Code *c, TRect W); void Clip(PointType *p1, PointType *p2, TRect W, bool *visible); void DrawElement(CDC* pDC, PointType p, int Num, TRect W, bool All); void NestBee(CDC* pDC, PointType p); void RightTurn(double g); void LeftTurn(double g); void LineForward(CDC* pDC, double a); void PolySpiral(CDC* pDC, double dist, double angle, double incr, int num); Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 114 void Six_Gon(CDC* pDC, PointType p, double dist, double angle, double incr, int num); void FillColor(CDC* pDC, int x, int y, unsigned int Color, unsigned int BoundColor); void ArcReal(CDC* pDC, PointType Center, double Radius, double StartAngle, double StopAngle); void SetPoint(double x, double y, PointType *p); void Circle(CDC* pDC, PointType Center, double Radius); void AmDuong(CDC* pDC, PointType p, double Radius); void NewPoint(PointType C, PointType B, PointType *B1); double GetAngle(PointType p1, PointType p2); void InterSect(PointType A, PointType B, PointType C, PointType D, PointType *CutP, TIntersect *CutKind); int LineOn(PointType C, PointType A, PointType B); void FiveBugsTile(CDC* pDC, double R, PointType Center, double g); void LineToReal(CDC* pDC, PointType p); void MoveToReal(CDC* pDC, PointType p); void LineReal(CDC* pDC, PointType p1, PointType p2); void Draw_Polygon(CDC* pDC,TPolygon plg); void MakeNGon(int n, double Radius, double g0, PointType Center, TPolygon *N_gon); void N_Turtlel_House(CDC* pDC, unsigned int solanLap, double d1, double d2, PointType diembd); void DragonCurves(CDC* pDC, PointType diemDau, PointType diemCuoi, unsigned int solanLap); void CCurves(CDC* pDC, PointType diemDau, PointType diemCuoi, unsigned int solanLap); void Rosettes(CDC * pDC, double bkinh, PointType Center, unsigned int sodinh); //{{AFX_MSG(CPatternsView) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; #ifndef _DEBUG // debug version in patternsView.cpp inline CPatternsDoc* CPatternsView::GetDocument() { return (CPatternsDoc*)m_pDocument; } #endif ///////////////////////////////////////////////////////////////////////////// Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 115 //{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional declarations immediately before the previous line. #endif // !defined(AFX_PATTERNSVIEW_H__0AD4B2CD_E2E7_11D2_9C9A_444553540000__IN CLUDED_) ndc.h #include #include const int MaxPoints = 250; const double PI = 3.1415; const double RADS = PI/180; // dung de doi degree sang radian typedef struct { double x; double y; } PointType; typedef struct { int Num; PointType Points[MaxPoints]; } TPolygon; typedef struct { // Ve Rua PointType CP; // Vi tri hien hanh Current Point double CD; // Huong hien hanh,Current direction } TTurle; typedef struct { double Left,Top,Right,Bottom; } TRect; typedef struct { // Ma nua vung Haft Space Code bool L,T,R,B; } Half_Space_Code; Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 116 typedef struct { PointType Child_p; double Child_w; //do rong double Child_a; //goc se ve bool Child_dir; //huong phai,trai } TArr; typedef enum {none, one, many} TIntersect; // 2 doan thang co the khong cat //nhau cat nhau, trung nhau Luaän AÙn Toát Nghieäp Phaàn thuyeát minh Sinh vieân thöïc hieän: Bieän Coâng Minh - Nguyeãn Xuaân Sôn Trang 117 Taøi lieäu tham khaûo 1. Computer Graphic - Donald Hearn - M. Pauline Baker 2. Using Visual C++ 5 - Kate Gregory 3. Inside Visual C++ - David J. Kruglinski 4. Visual C++ Programming – Steven Holzner 5. Tech yourself Visual C++ 2 in 21 days – Namir Clement Shammas 6. Professional MFC Visual C++ 5 – Mike Blaszczak 7. Ñoà hoïa maùy tính - Nguyeãn Quoác Cöôøng & Hoaøng Quoác Haûi 8. Giaùo trình ñoà hoïa maùy tính - Döông Anh Ñöùc & Leâ Ñöùc Duy 9. Ñoà hoïa maùy tính trong ngoân ngöõ C – Toáng Nghóa & Hoaøng Ñöùc Haûi

Các file đính kèm theo tài liệu này:

  • pdfCơ sở toán học trong đồ hoạ máy tính.pdf
Luận văn liên quan