Anda di halaman 1dari 11

Hinh03 DEF.

H
#ifndef _DEF_H #define _DEF_H typedef int bool; extern const bool false, true; inline bool betw(double a, double b, double x) { return x >= a && x <= b; } inline int round(double x) { return x >= 0 ? int(x+0.5) : int(x-0.5); } #endif

DEF.CPP
#include "def.h" const bool false = 0, true = 1;

DIEM.H
#include #include #include #include <iostream.h> <graphics.h> <conio.h> "dohoa.h"

DIEM.CPP
#include #include #include #include <iostream.h> <graphics.h> <conio.h> "dohoa.h"

class Diem { double x, y; public: Diem (double xx = 0, double yy = 0):x(xx), y(yy){} void Set(double xx, double yy) {x = xx; y = yy;} double GetX() const {return x;} double GetY() const {return y;} void Nhap(); void Xuat() const; Diem Cong(Diem b){return Diem(x+b.x,y+b.y);} Diem Tru(Diem b) {return Diem(xb.x,y-b.y);} void TinhTien(double dx, double dy) { x += dx; y += dy; } void Ve(int color = WHITE) const; double KhoangCach(Diem b); }; inline void Diem::Nhap() { cin >> x >> y; } inline void Diem::Xuat() const { cout <<"(" << x << "," << y << ")"; } inline Diem operator + (Diem a, Diem b) { return a.Cong(b); } inline Diem operator - (Diem a, Diem b) { return a.Tru(b); } inline void Diem::Ve(int color) const { textcolor(color); SetPixel(x,y); }

class Diem { double x, y; public: Diem (double xx = 0, double yy = 0):x(xx), y(yy){} void Set(double xx, double yy) {x = xx; y = yy;} double GetX() const {return x;} double GetY() const {return y;} void Nhap(); void Xuat() const; Diem Cong(Diem b){return Diem(x+b.x,y+b.y);} Diem Tru(Diem b) {return Diem(xb.x,y-b.y);} void TinhTien(double dx, double dy) { x += dx; y += dy; } void Ve(int color = WHITE) const; double KhoangCach(Diem b); }; inline void Diem::Nhap() { cin >> x >> y; } inline void Diem::Xuat() const { cout <<"(" << x << "," << y << ")"; } inline Diem operator + (Diem a, Diem b) { return a.Cong(b); } inline Diem operator - (Diem a, Diem b) { return a.Tru(b); } inline void Diem::Ve(int color) const { textcolor(color); SetPixel(x,y); }

VUONG.H
#include "def.h" #include "hcn.h" class HV:public HCN { public: HV(Diem tt, double canh):HCN(tt, canh, canh){} HV(double ttx, double tty, double canh):HCN(ttx,tty,canh,canh){} HV():HCN(7,8,6,6){} char *TenLop() {return "Hinh Vuong";} };

DOHOA.CPP
#ifndef _HINH_H #define _HINH_H #include "def.h" #include "diem.h" class Hinh { public: virtual double DienTich() const = NULL; virtual void TinhTien(double dx, double dy) = NULL; virtual void Ve(int color = WHITE) const = NULL; virtual char *TenLop() {return "Hinh";} virtual bool NamTrong(Diem d) const = NULL; virtual void PhongTo(double tiLe) = NULL; virtual Diem Upl() const = NULL; virtual Diem Lwr() const = NULL; }; void VeDs(Hinh *ah[], int n, int color); void VeDs(Hinh *ah[], int n, int aColor[]); void TinhTienDs(Hinh *ah[], int n, double dx, double dy); void ViTuDs(Hinh *ah[], int n, double tiLe); double TongDienTich(Hinh *ah[], int n); #endif

VUONG.CPP
#include "vuong.h"

DOHOA.H
#ifndef _DOHOA_H #define _DOHOA_H #include <conio.h> extern const int MAXX, MAXY; inline void SetPixel(int x, int y) { if (x >= 0 && x <= MAXX && y >= 0 && y <= MAXY) { gotoxy(2*x+1, y+1); cprintf("*"); } } inline void textxy(int x, int y, char *buf) { gotoxy(2*x+1, y+1); cprintf(buf); } inline void textxy(int x, int y, char *buf, int color) { gotoxy(2*x+1, y+1); textcolor(color); cprintf(buf); } void Moveto(int x, int y); void Lineto(int x, int y); void Arcto(int x, int y, double bulge); #endif

HINH.H
#ifndef _HINH_H #define _HINH_H #include "def.h" #include "diem.h" class Hinh { public: virtual double DienTich() const = NULL; virtual void TinhTien(double dx, double dy) = NULL; virtual void Ve(int color = WHITE) const = NULL; virtual char *TenLop() {return "Hinh";} virtual bool NamTrong(Diem d) const = NULL; virtual void PhongTo(double tiLe) = NULL; virtual Diem Upl() const = NULL; virtual Diem Lwr() const = NULL; }; void VeDs(Hinh *ah[], int n, int color); void VeDs(Hinh *ah[], int n, int aColor[]); void TinhTienDs(Hinh *ah[], int n, double dx, double dy); void ViTuDs(Hinh *ah[], int n, double tiLe); double TongDienTich(Hinh *ah[], int n); #endif

HINH.CPP
#include <graphics.h> #include "hinh.h" void VeDs(Hinh *ah[], int n, int color) { for (int i = 0; i < n; i++) ah[i]->Ve(color); } void VeDs(Hinh *ah[], int n, int aColor[]) { for (int i = 0; i < n; i++) ah[i]->Ve(aColor[i]); } void TinhTienDs(Hinh *ah[], int n, double dx, double dy) { for (int i = 0; i < n; i++) ah[i]->TinhTien(dx,dy); } /* void ViTuDs(Hinh *ah[], int n, double tiLe) { for (int i = 0; i < n; i++) ah[i]->ViTu(tiLe); } */ double TongDienTich(Hinh *ah[], int n) { double S = 0; for (int i = 0; i < n; i++) S += ah[i]->DienTich(); return S; }

HCN.H
#ifndef _HCN_H #define _HCN_H #include "hinh.h" class HCN:public Hinh { Diem TrenTrai; double rong, cao; public: HCN(Diem tt, double r, double c):TrenTrai(tt), rong(r), cao(c){} HCN(double ttx, double tty, double r, double c):TrenTrai(ttx,tty), rong(r), cao(c){} HCN():TrenTrai(4,6), rong(7), cao(4){} virtual double DienTich() const {return rong*cao;} void TinhTien(double dx, double dy) {TrenTrai.TinhTien(dx,dy);} void Ve(int color = WHITE) const; bool NamTrong(Diem d) const; char *TenLop() {return "Hinh Chu Nhat";} virtual Diem Upl() const {return TrenTrai;} virtual Diem Lwr() const {return TrenTrai+Diem(rong,cao);} virtual void PhongTo(double tiLe); }; #endif

HCN.CPP
#ifndef _TGV_H #define _TGV_H #include "hinh.h" class TGV:public Hinh { Diem Dinh; double canh; public: TGV(Diem d, double c):Dinh(d), canh(c){} TGV(double x, double y, double c):Dinh(x,y),canh(c){} TGV():Dinh(2,18), canh(9){} virtual double DienTich() const {return 0.5*canh*canh;} void TinhTien(double dx, double dy) {Dinh.TinhTien(dx,dy);} void Ve(int color = WHITE) const; bool NamTrong(Diem d) const; char *TenLop() {return "Tam giac vuong";} virtual Diem Upl() const {return Dinh-Diem(0,canh);} virtual Diem Lwr() const {return Dinh+canh;} virtual void PhongTo(double tiLe); }; #endif

TGV.H
#ifndef _TGV_H #define _TGV_H #include "hinh.h" class TGV:public Hinh { Diem Dinh; double canh; public: TGV(Diem d, double c):Dinh(d), canh(c){} TGV(double x, double y, double c):Dinh(x,y),canh(c){} TGV():Dinh(2,18), canh(9){} virtual double DienTich() const {return 0.5*canh*canh;} void TinhTien(double dx, double dy) {Dinh.TinhTien(dx,dy);} void Ve(int color = WHITE) const; bool NamTrong(Diem d) const; char *TenLop() {return "Tam giac vuong";} virtual Diem Upl() const {return Dinh-Diem(0,canh);} virtual Diem Lwr() const {return Dinh+canh;} virtual void PhongTo(double tiLe); }; #endif

TGV.CPP
#include <conio.h> #include "dohoa.h" #include "tgv.h" void TGV::Ve(int color) const { textcolor(color); int x = Dinh.GetX(), y = Dinh.GetY(); for (int i = x; i <= x+canh; i++) SetPixel(i,y); for (int j = 1; j <= canh; j++) { SetPixel(x,y-j); SetPixel(x+canh-j,y-j); } } bool TGV::NamTrong(Diem d) const { int x = d.GetX()-Dinh.GetX(), y = d.GetY()-Dinh.GetY(); return betw(0, canh, x) && betw(0, canh-x, -y); } void TGV::PhongTo(double tiLe) { canh *= tiLe; }

T_HCN.CPP
#include <conio.h> #include "hcn.h" void main() { int c; clrscr(); HCN h; h.Ve(YELLOW); Diem T(10,12); T.Ve(); gotoxy(1,1); cout << (h.NamTrong(T) ? "Nam trong" : "Khong Nam trong"); do { c = getch(); if (c == 0) { switch(c = getch()) { case 75: h.TinhTien(-1,0); break; case 77: h.TinhTien(1,0); break; case 72: h.TinhTien(0,-1); break; case 80: h.TinhTien(0,1); break; } clrscr(); h.Ve(YELLOW); T.Ve(); gotoxy(1,1); cout << (h.NamTrong(T) ? "Nam trong" : "Khong Nam trong"); } } while (c!= 27); getch(); }

T_VUONG.CPP
#include <conio.h> #include "vuong.h" void main() { int c; clrscr(); HV v; HCN cn; cn.Ve(GREEN); v.Ve(YELLOW); Diem T(10,12); T.Ve(); gotoxy(1,1); cout << (cn.NamTrong(T) ? "Nam trong HCN\n" : "Khong Nam trong HCN\n"); cout << (v.NamTrong(T) ? "Nam trong HV\n" : "Khong Nam trong HV\n"); do { c = getch(); if (c == 0) { switch(c = getch()) { case 75: T.TinhTien(-1,0); break; case 77: T.TinhTien(1,0); break; case 72: T.TinhTien(0,-1); break; case 80: T.TinhTien(0,1); break; } clrscr(); cn.Ve(GREEN); v.Ve(YELLOW); T.Ve(); gotoxy(1,1); cout << (cn.NamTrong(T) ? "Nam trong HCN\n" : "Khong Nam trong HCN\n"); cout << (v.NamTrong(T) ? "Nam trong HV\n" : "Khong Nam trong HV\n"); } } while (c!= 27); getch(); }

TDM.CPP
#include <conio.h> #include "diem.h" void main() { int c; clrscr(); Diem T(10, 5); T.Ve(RED); do { c = getch(); if (c == 0) { switch(c = getch()) { case 75: T.TinhTien(-1,0); break; case 77: T.TinhTien(1,0); break; case 72: T.TinhTien(0,-1); break; case 80: T.TinhTien(0,1); break; } clrscr(); T.Ve(YELLOW); } } while (c!= 27); getch(); }

THINH.CPP
#include #include #include #include #include #include #include #include <stdlib.h> <stdio.h> <conio.h> <math.h> <string.h> "dohoa.h" "hinh.h" "vuong.h"

#include "tgv.h" // const bool false = 0, true = 1; void GetInfo(Hinh *a[], int n, Diem d, char s[]) { char t[255]; bool bInside = false; sprintf(s, "Diem nam trong"); for (int i = 0; i < n; i++) if (a[i]->NamTrong(d)) { bInside = true; sprintf(t, " hinh %d(%s)", i+1, a[i]->TenLop()); strcat(s, t); } if (!bInside) strcpy(s, "Diem khong nam trong hinh nao"); } void main() { clrscr(); char buf[255]; char hd[] = "Bam t,g de doi hinh hien hanh, mui ten de di chuyen"; Hinh *ah[3]; ah[0] = new HCN;//(5,12,8,6); ah[2] = new TGV; ah[1] = new HV; int n = 3; Diem d(10,10); VeDs(ah,n,GREEN); d.Ve(YELLOW); int ch = 0; int i; GetInfo(ah,n,d,buf); textxy(0,0,buf,WHITE); textxy(10,24,hd,WHITE); bool bRedraw = false; int h = 0; while (ch != 27) { ch = getch(); switch(ch)

{ case 't': if (++h == n+1) h = 0; break; case 'g': if (--h < 0) h = n; break; case '+': if (h < n) { ah[h]->PhongTo(1.2); bRedraw = true; break; } case '-': if (h < n) { ah[h]->PhongTo(1/1.2); bRedraw = true; } break; case 0: ch = getch(); cout << ch; if (h < n) switch(ch) { case 72: ah[h]->TinhTien(0,-1); bRedraw = true; break; case 80: ah[h]->TinhTien(0,1); bRedraw = true; break; case 75: ah[h]->TinhTien(-1,0); bRedraw = true; break; case 77: ah[h]->TinhTien(1,0); bRedraw = true; break; } else switch(ch) { case 72: d.TinhTien(0,-1); bRedraw = true; break; case 80: d.TinhTien(0,1); bRedraw = true; break; case 75: d.TinhTien(-1,0); bRedraw = true; break; case 77: d.TinhTien(1,0); bRedraw = true;

break; } } if (bRedraw) { clrscr(); VeDs(ah,n,GREEN); d.Ve(YELLOW); GetInfo(ah,n,d,buf); textxy(0,0,buf,WHITE); textxy(10,24,hd,WHITE); bRedraw = false; } } getch(); }

Anda mungkin juga menyukai