#include "stdafx.h" #include #include #include class triangle { int color_of_screen[3], color_of_line[3],type_of_line,thickness_of_line,color_of_fill[3], type_of_fill ; int x1,x2,y1,y2,x3,y3; public: void set_coordinate(int, int, int, int, int, int); void set_screen(int[]); void set_line(int [],int); void set_fill(int []); void get_coordinate(int*, int*, int*, int*,int*,int*); void get_screen(int []); void get_line(int [],int*); void get_fill(int [], int*); void draw_triangle(HDC); void moving(int,int); int save(); int load(); int prov(); void print_parametrs(); }; void triangle::set_coordinate(int a1, int b1, int a2, int b2, int a3, int b3) { x1=a1; y1=b1; x2=a2; y2=b2; x3=a3; y3=b3; } void triangle::set_line(int color[], int thickness) { for(int i=0; i<3;i++) color_of_line[i]=color[i]; thickness_of_line=thickness; } void triangle::set_fill(int color[3]) { for(int i=0; i<3; i++) color_of_fill[i]=color[i]; } void triangle::set_screen(int color[]) { for(int i=0; i<3;i++) color_of_screen[i]=color[i]; } void triangle::get_coordinate(int *a1, int *b1, int *a2, int *b2, int *a3, int *b3) { *a1=x1; *b1=y1; *a2=x2; *b2=y2; *a3=x3; *b3=y3; } void triangle::get_line(int color[], int *thickness) { for(int i=0; i<3;i++) color[i]=color_of_line[i]; *thickness=thickness_of_line; } void triangle::get_fill(int color[], int *type) { for(int i=0; i<3;i++) color[i]=color_of_fill[i]; *type=type_of_fill; } void triangle::get_screen(int color[]) { for(int i=0; i<3; i++) color[i]=color_of_screen[i]; } void triangle::draw_triangle(HDC hdc) { HPEN hPen; HBRUSH hBrush; hPen = CreatePen(PS_SOLID, thickness_of_line, RGB(color_of_line[0],color_of_line[1],color_of_line[2])); HPEN hOldPen = SelectPen (hdc, hPen); hBrush = CreateSolidBrush(RGB(color_of_fill[0],color_of_fill[1],color_of_fill[2])); HBRUSH hOldBrush = SelectBrush(hdc, hBrush); POINT ppt[3]={{x1,y1},{x2,y2},{x3,y3}}; Polygon(hdc,ppt,3); SelectPen(hdc, hOldPen); SelectBrush(hdc, hOldBrush); DeletePen(hPen); DeleteBrush(hBrush); } void triangle::moving(int dx,int dy) { x1=x1+dx; y1=y1+dy; x2=x2+dx; y2=y2+dy; x3=x3+dx; y3=y3+dy; } int triangle::prov() { if((x1==y2)&&(x2==y1) && (x3==y3)) return 0; else return 1; } int triangle::save()// сохранение фигуры в файл { FILE *fileIN; int i; char filename[10]; printf("\nEnter file name: "); scanf_s("%s", &filename); if((fileIN=fopen(filename,"w")) == NULL) { getch(); return 0;} fprintf(fileIN,"\n%d %d %d %d",x1,y1,x2,y2,x3,y3); fprintf(fileIN, "\n"); for(i=0;i<3;i++){ fprintf (fileIN,"%d ",color_of_screen[i]); } fprintf(fileIN, "\n"); for(i=0; i<3; i++){ fprintf (fileIN,"%d ",color_of_line); } fprintf (fileIN,"\n%d",thickness_of_line); fprintf(fileIN, "\n"); for(i=0; i<3;i++) fprintf (fileIN,"%d ",color_of_fill[i]); fclose(fileIN); return 1; } int triangle::load()// загрузка фигуры с файла { FILE *fileOUT; int i; char filename[10]; printf("\n Enter file name: "); scanf("%s", &filename); if((fileOUT=fopen(filename,"r")) == NULL) { getch(); return 0;} fscanf(fileOUT," %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3); for(i=0;i<3;i++) fscanf (fileOUT,"%d",&color_of_screen[i]); for(i=0;i<3;i++) fscanf (fileOUT,"%d", &color_of_line[i]); fscanf (fileOUT,"%d", &thickness_of_line); for(i=0;i<3;i++) fscanf (fileOUT,"%d", &color_of_fill[i]); fclose(fileOUT); return 1; } void triangle:: print_parametrs()// вывод параметров на экран { int i; printf("First point=(%d;%d)\nSecond point=(%d;%d)\nThird point=(%d;%d)\n",x1,y1,x2,y2,x3,y3); printf("\nColor of the screen: "); for(i=0;i<3;i++) printf ("%d ",color_of_screen[i]); printf("\nColor of line: "); for(i=0;i<3;i++) printf ("%d ", color_of_line[i]); printf ("\nThikness of line=%d", thickness_of_line); printf("\nColor of fill: "); for(i=0;i<3;i++) printf ("%d ", color_of_fill[i]); printf ("\n"); getch(); } int control1 (triangle *sq, RECT r, HWND hwnd)//проверка на вложенность в экран { int f=0; GetClientRect(hwnd, &r); int x1,y1,x2,y2,x3,y3; sq->get_coordinate(&x1,&y1,&x2,&y2,&x3,&y3); if( (x1>r.left) && (x2>r.left) && (x3>r.left) && (x1r.top) && (y2>r.top)&& (y3>r.top) && (y1get_coordinate(&x1,&y1,&x2,&y2,&x3,&y3); return f; } void SaveInfo (triangle sq1, triangle sq2) { system("cls"); printf("\nSave info\n"); sq1.save(); sq2.save(); } int LoadInfo(triangle *sq1,triangle *sq2,HWND hwnd ) { RECT r; GetClientRect(hwnd, &r); system("cls"); printf("\nInfo Load\n"); if( !sq1->load() )return 0; sq1->print_parametrs(); if(control3(sq1)==1) {printf("\n It's not a triangle"); getch(); return 1;} if( !sq2->load() ) return 0; sq2->print_parametrs(); if(control3(sq2)==1) {printf("\n It's not a triangle"); getch(); return 1;} if((control1(sq1,r, hwnd))==1) return 1; else return 0; } void Printr(triangle sq1,triangle sq2,HDC hdc,RECT r, HWND hwnd) { system("cls"); sq1.draw_triangle(hdc); sq2.draw_triangle(hdc); } int clrcls(HDC hdc) { HBRUSH hBrush; hBrush = CreateSolidBrush(RGB(0,0,0)); HBRUSH hOldBrush = SelectBrush(hdc, hBrush); Rectangle(hdc, 640, 300, 0, 0); SelectBrush(hdc, hOldBrush); DeleteBrush(hBrush); return 0; } void Animation(triangle *sq1,triangle *sq2,HWND hwnd, HDC hdc) { RECT r; GetClientRect(hwnd, &r); system("cls"); int dx,dy; printf("\n dx="); scanf("%d", &dx); printf("\n dy="); scanf("%d", &dy); Printr(*sq1,*sq2,hdc,r,hwnd); sq1->moving(dx,dy); sq2->moving(dx,dy); if ((control1(sq1,r,hwnd))==0) {sq1->moving(-dx,-dy); sq2->moving(-dx,-dy); system("cls"); printf("\nError:triangle miss from screen\nPress any key"); getch(); } else { clrcls(hdc); Printr(*sq1,*sq2,hdc,r,hwnd); getch();} } int _tmain() { char buf[100]; USES_CONVERSION; HWND hwnd = FindWindow(A2W("ConsoleWindowClass"), NULL); HDC hdc = GetDC(hwnd); RECT r; int i =0; GetClientRect(hwnd, &r); SetBkColor (hdc, RGB(0, 0, 0)); SetTextColor (hdc, RGB(255, 0, 0)); triangle sq1,sq2; int exit = 0, vvod = 0; while(exit == 0) { printf("\n ENTER YOUR CHOICE! "); printf("\n1. Enter unformation about rectangle!"); printf("\n2. Load information"); printf("\n3. Saving information"); printf("\n4. Paint empty rectange 1"); printf("\n5. Paint full rectangle! "); printf("\n6. Paint 2 rectangles!"); printf("\n7. Moving of the rectangles!"); printf("\n8. Exit"); if(!vvod)printf("\nData NO in\n"); switch(getche()) { case '1':{system("cls");getch();}break; case '2':if(LoadInfo(&sq1, &sq2,hwnd))vvod = 1; break; // ввод фигуры с файла case '3':if(vvod)SaveInfo(sq1,sq2); break; // соранение фигуры case '4':if(vvod){ // не закрашенная фигура system("cls"); sq1.draw_triangle(hdc); sprintf_s(buf, "triangle!!!"); TextOut(hdc, 180, 20,A2W(buf),strlen(buf)); getch(); clrcls(hdc); } break; case '5':if(vvod){ // закрашенная фигура system("cls"); sq2.draw_triangle(hdc); sprintf_s(buf, "Full triangle!"); TextOut(hdc, 180, 20,A2W(buf),strlen(buf)); getch(); clrcls(hdc); } break; case '6':if(vvod){ // вложенные фигуры system("cls"); if(control2(sq1,sq2,r)==0) { Printr(sq1,sq2,hdc,r,hwnd); } else printf("\nFigury ne vlogeny!"); sprintf_s(buf, "2 triangle!"); TextOut(hdc, 180, 20,A2W(buf),strlen(buf)); getch(); clrcls(hdc); } break; case '7':{if(vvod) Animation(&sq1, &sq2,hwnd, hdc); clrcls(hdc);} break; // перемещение фигуры case '8':exit = 1; break; } system("cls"); } }