-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rect Gen V4.0.cpp
192 lines (154 loc) · 4.82 KB
/
Rect Gen V4.0.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#define MAX_RECTS 25
#define MPW_BOT 0
#define MPW_TOP 600
#define MPW_LEFT 0
#define MPW_RIGHT 800
struct Rectangle
{
char Name[5];
int Bot;
int Top;
int Left;
int Right;
};
//Random Number---------------------------------------------------------------------------------------
int RandNum (int CharMin, int CharMax)
{
return rand() % (CharMax - CharMin + 1) + CharMin;
}
//Menu For thing. Dont Really know if I needed to Make this. But W/E---------------------------------------
int menu()
{
printf("\n\n\n1 = Manual Input New Rect \n2 = Delete Existing Rect \n3 = Find Intersection \n4 = Find Union \n5 = Sort By Name \n6 = Point In Rectangle \n7 = Quit\n\nSelection? : ");
return (getch());
}
//Generate Rectangles_--------------------------------------------------------
Rectangle GenRect()
{
struct Rectangle RandomRect;
RandomRect.Bot = RandNum (MPW_BOT,MPW_TOP-1);
RandomRect.Top = RandNum (RandomRect.Bot+1,MPW_TOP);
RandomRect.Left = RandNum (MPW_LEFT,MPW_RIGHT-1);
RandomRect.Right = RandNum (RandomRect.Left+1,MPW_TOP);
for (int i=0; i<4; i++)
{
RandomRect.Name[i] = RandNum ('a','z');
}
RandomRect.Name[4] = 0;
return (RandomRect);
}
//Manual Input--------------------------------------------------------------------
Rectangle ManualRect ()
{
struct Rectangle RandomRect;
printf("\n\nEnter the rectangle bottom: ");
scanf("%i", &RandomRect.Bot);
printf("\n\nEnter the rectangle top: ");
scanf("%i", &RandomRect.Top);
printf("\n\nEnter the rectangle left: ");
scanf("%i", &RandomRect.Left);
printf("\n\nEnter the rectangle right: ");
scanf("%i", &RandomRect.Right);
printf("\n\nEnter name. Cuts off if more than 4 chars ");
scanf("%s", &RandomRect.Name);
RandomRect.Name[4] = 0;
return(RandomRect);
}
//Point Within?
string PointWithin(Rectangle Rect1,int X,int Y){
if (X>Rect1.Left && X<Rect1.Right && Y > Rect1.Bot && Y < Rect1.Top)
return ("The point is within the rectangle.");
return ("The point is not within the rectangle.");
}
//Show Rects.------------------------------------------------------------------
void PrintRect (Rectangle RandomRect[MAX_RECTS], int NumBoxes)
{
for (int i=0; i < NumBoxes; i++ )
{
printf("\n%i %s = Bottom Left %i , %i Top Right %i , %i",i+1,RandomRect[i].Name, RandomRect[i].Left, RandomRect[i].Bot, RandomRect[i].Right, RandomRect[i].Top);
}
}
// Find Intersection Points
void Intersection (Rectangle Rect2,Rectangle Rect1)
{
if (Rect1.Left < Rect2.Right && Rect1.Bot < Rect2.Top && Rect2.Left < Rect1.Right && Rect2.Bot < Rect1.Top || Rect2.Left < Rect1.Right && Rect2.Bot < Rect1.Top && Rect1.Left < Rect2.Right && Rect1.Bot < Rect2.Top)
printf("The resulting rectangles points are: %i , %i %i , %i",(Rect1.Left < Rect2.Left)?Rect2.Left:Rect1.Left, (Rect1.Bot < Rect2.Bot)?Rect2.Bot:Rect1.Bot, (Rect1.Right > Rect2.Right)?Rect2.Right:Rect1.Right, (Rect1.Top > Rect2.Top)?Rect2.Top:Rect1.Top);
}
//Main Function--------------------------------------------------------------------------------
int main()
{
srand(time (NULL));
struct Rectangle RandomRect[MAX_RECTS];
Rectangle r;
bool Run = true;
int NumBoxes= 100;
int TempNum,TempNum2,TempNum3;
//How many you want?
while (NumBoxes>25)
{
printf(" How many boxes you want? (max 25): ");
scanf("%i",&NumBoxes);
}
//Initialize Boxes
for (int i=0; i < MAX_RECTS; i++)
{
RandomRect[i] = GenRect();
}
//Main Run Loop
while (Run=true)
{
printf("%i",NumBoxes);
PrintRect(RandomRect, NumBoxes);
switch (menu())
{
case '1':
printf("\n\nEnter the rectangle number you'd like to write to: ");
scanf("%i",&TempNum);
if (TempNum>NumBoxes)
{
NumBoxes+=1;
RandomRect[NumBoxes-1]=ManualRect();
} else {
RandomRect[TempNum-1]=ManualRect();
}
break;
case '2':
printf("\n\nEnter the rectangle number you'd like to delete: ");
scanf("%i",&TempNum);
for (int i=0; i < (NumBoxes); i++)
{
RandomRect[TempNum-2+i]=RandomRect[TempNum-1+i];
}
NumBoxes-=1;
break;
case '3':
printf("\n\nEnter the first rectangle to compare: ");
scanf("%i",&TempNum);
printf("\n\nEnter the second rectangle to compare: ");
scanf("%i",&TempNum2);
Intersection(RandomRect[TempNum-1],RandomRect[TempNum2-1]);
break;
case '6':
printf("\n\nEnter the X coodinate: ");
scanf("%i",&TempNum);
printf("\n\nEnter the X coodinate: ");
scanf("%i",&TempNum2);
printf("\n\nEnter the rectangle to compare: ");
scanf("%i",&TempNum3);
printf("\n%s",PointWithin(RandomRect[TempNum3],TempNum,TempNum2))
case '7':
Run=false;
break;
default:
printf("\n Either invalid selection or not yet implemented feature :(");
}
}
getch();
return 0;
}