-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
58 lines (48 loc) · 1.72 KB
/
Main.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
#include <iostream>
#include "random.h"
#include "gevents.h"
#include "gtypes.h"
#include "gwindow.h"
using namespace std;
#define NUM_POINTS 3
#define CIRCLE_RADIUS 0.5
// draws a filled circle around the given point
void drawFilledCircle(GWindow &window, GPoint point, double circleRadius)
{
window.fillOval(point.getX() - circleRadius, point.getY() - circleRadius,
circleRadius * 2, circleRadius * 2);
}
int main() {
GWindow gw;
gw.setCloseOperation(GWindow::CLOSE_EXIT);
GPoint pts[NUM_POINTS]; // A, B, and C
int currentPointIndex = 0;
while (true) {
GMouseEvent e = waitForEvent(MOUSE_PRESSED);
if (e.getEventType() == MOUSE_PRESSED) {
pts[currentPointIndex++] = GPoint(e.getX(), e.getY());
if (currentPointIndex == NUM_POINTS)
break;
}
}
// draw triangle
gw.setColor("BLACK");
for (int i = 0; i < NUM_POINTS; i++)
gw.drawLine(pts[i], pts[(i+1)%3]);
// randomly choose one vertex as the current point
GPoint currentPoint = pts[randomInteger(0, NUM_POINTS-1)];
while (true) {
// draw a small filled circle around the current point
drawFilledCircle(gw, currentPoint, CIRCLE_RADIUS);
// randomly choose one vertex
GPoint randomVertex = pts[randomInteger(0, NUM_POINTS-1)];
// move currentPoint half of the distance toward randomVertex
currentPoint = GPoint((currentPoint.getX() + randomVertex.getX()) / 2,
(currentPoint.getY() + randomVertex.getY()) / 2);
// stop when user clicks the mouse
GEvent e = getNextEvent();
if (e.getEventType() == MOUSE_PRESSED)
break;
}
return 0;
}