include stdafx include math include windowsx include AtlConv include c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "stdafx.h"
#include <math.h>
#include <windowsx.h>
#include <AtlConv.h>
#include <conio.h>
struct _points{int x,y;}; //Четыре точки трапеции
//Считывание данных
void scanpoints(FILE *fp,_points *points)
{
int point_x=0,point_y=0;
for(int i=0;fscanf(fp,"%d,%d",&point_x,&point_y)!=EOF&&i<8;i++)
{
points[i].x=point_x;
points[i].y=point_y;
}
}
int parallel(_points p_1,_points p_2,_points p_3,_points p_4)
{
float k_1=0.,k_2=0.; //Угловой коэффициент прямой проходящей через p_1 и p_2
bool result;
k_1=(float)(p_2.y-p_1.y)/(p_2.x-p_1.x);
k_2=(float)(p_4.y-p_3.y)/(p_4.x-p_3.x);
if(abs(k_1)==abs(k_2))
result=true;
else
result=false;
return result;
}
int inwindow(RECT size,_points *points)
{
bool result=true;
int i;
for(i=0;i<4;i++)
if(!(points[i].x>size.left&&points[i].x<size.right&&points[i].y>size.top&&points[i].y<size.bottom))
result=false;
return result;
}
void brushelement(HWND hwnd,HDC hdc,_points *points,int type,COLORREF pcolor,int bcolor)
{
POINT draw_point[4];
HPEN hPen=CreatePen(PS_SOLID,1,pcolor); //Создание пера
HPEN hOldPen=SelectPen(hdc,hPen); //Сохранение старого пера
HBRUSH hBrush=GetStockBrush(bcolor);
HBRUSH hOldBrush=SelectBrush(hdc,hBrush); //Сохранение старой кисти
for(int i=0;i<4;i++)
{
draw_point[i].x=points[i].x;
draw_point[i].y=points[i].y;
}
Polygon(hdc,draw_point,4); //Рисование трапеции
if(type==3)
{
for(int i=4,j=0;i<8;i++,j++)
{
draw_point[j].x=points[i].x;
draw_point[j].y=points[i].y;
}
HPEN hPen_1=CreatePen(PS_SOLID,1,pcolor); //Создание пера
HPEN hOldPen_1=SelectPen(hdc,hPen_1);
HBRUSH hBrush_1=GetStockBrush(BLACK_BRUSH);
HBRUSH hOldBrush_1=SelectBrush(hdc,hBrush_1); //Сохранение старой кисти
Polygon(hdc,draw_point,4); //Рисование трапеции
SelectPen(hdc,hOldPen_1); //Активация старого пера
SelectBrush(hdc,hOldBrush_1); //Активация старой кисти
DeleteBrush(hBrush_1); //Удаление созданной кисти
DeletePen(hPen_1); //Удаление созданного пера
}
SelectPen(hdc,hOldPen); //Активация старого пера
SelectBrush(hdc,hOldBrush); //Активация старой кисти
DeletePen(hPen); //Удаление созданного пера
DeleteBrush(hBrush); //Удаление созданной кисти
}
int inside(_points *points)
{
bool result=true;
if(
points[4].x>points[0].x&&
points[4].y>points[0].y&&
points[5].x<points[1].x&&
points[5].y>points[1].y&&
points[6].x<points[2].x&&
points[6].y<points[2].y&&
points[7].x>points[3].x&&
points[7].y<points[3].y)
result=true;
else
result=false;
return result;
}
void main()
{
_points points[8];
FILE *fp=fopen("points.txt","r");
FILE *fp2=fopen("exit.txt","w");
USES_CONVERSION;
HWND hwnd=FindWindow(A2W("ConsoleWindowClass"),NULL);
HDC hdc=GetDC(hwnd);
if(hdc)
{
if(fp)
{
RECT rt;
GetClientRect(hwnd,&rt);
scanpoints(fp,points);
if(inwindow(rt,points))
{
//Является ли фигура трапецией?
if(parallel(points[0],points[1],points[3],points[2])^parallel(points[0],points[3],points[1],points[2]))
{
SetBkColor(hdc,RGB(0,54,0)); //Установка чёрного Background
int key=0;
while(key!=4)
{
scanf("%d",&key);
switch(key)
{
case 1:
brushelement(hwnd,hdc,points,1,RGB(255,255,255),BLACK_BRUSH); //Незакрашенная трапеция
break;
case 2:
brushelement(hwnd,hdc,points,2,RGB(255,255,255),DKGRAY_BRUSH); //Закрашенная трапеция
break;
case 3:
if(parallel(points[4],points[5],points[7],points[6])^parallel(points[4],points[7],points[5],points[6]))
{
if(inside(points))
{
brushelement(hwnd,hdc,points,3,RGB(255,255,255),DKGRAY_BRUSH); //Трапеция с дыркой
}
else
fprintf(fp2,"Error: the second figure is not inscribed in the first.");
}
else
fprintf(fp2,"Error: the second figure - not a trapeze, as opposite sides are not parallel.");
break;
default: key=4; break;
}
}
}
else
fprintf(fp2,"Error: the figure - not a trapeze, as opposite sides are not parallel.");
}
else
fprintf(fp2,"Error: the figure is outside the window.");
}
else
fprintf(fp2,"Error: file \"points.txt\" not found.");
ReleaseDC(hwnd,hdc);
}
else
fprintf(fp2,"Error: hdc not load.");
}