Hey,

Here is a program I made which has interested quite a lot of people over the past few weeks, so I figured I would share it :)
Using a small amount of trigonometry you can create some very fascinating shapes and effects. But in order to draw anything you need a surface to draw on, so to keep the code short and simple, instead of making my own window, it will open mspaint.exe and emulate some mouse activity (mouse clicking / moving). Before you start the application make sure that mspaint's drawing area fills the window or it may not work fully.

For some intresting shapes try the following inputs:

Spiral
Angle Speed: 10
Frame interval: 15

Box
Angle Speed: 90
Frame interval: 15

Curved Box
Angle Speed: 91
Frame interval: 15

Triangular
Angle Speed: 120
Frame interval: 15

Triangular curved
Angle Speed: 121
Frame interval: 15

Random
Angle Speed: 156
Frame interval: 15

``````#include<cmath>		// For trig functions
#include<iostream>	// For IO functions
#include<windows.h>	// For windows functions
using namespace std;

#define PI	3.14159

// Disable uninitialized local variable warning
#pragma warning(disable:4700)

int main() {
double angleSpeed;
int interval;

cout << "Enter number of degrees to rotate every frame:\n> ";
cin >> angleSpeed;

cout << "\nEnter radius speed (number of pixels to move away from centre) every frame:\n> ";

cout << "\nEnter interval for each frame:\n> ";
cin >> interval;

// Open mspaint.exe
system("start mspaint.exe");

// Find the handle for mspaint
HWND paint;
do {
// Try and find window, if not found, wait 100ms and try again
paint = FindWindow("MSPaintApp", "Untitled - Paint");
Sleep(100);
} while (paint == NULL);

Sleep(1000);

// Get rect of window
RECT r;
GetWindowRect(paint, &r);

// Get rect of client
RECT cr;
GetClientRect(paint, &cr);

// Find centre of window
int cx = r.left + (cr.right / 2);
int cy = r.top + (cr.bottom / 2);

// Move mouse to centre of window
SetCursorPos(cx,cy);

// Find paint's client window
POINT p;
GetCursorPos(&p);
paint = WindowFromPoint(p);

// Validate rects
GetWindowRect(paint, &r);
GetClientRect(paint, &cr);

// Get centre
cx = r.left + (cr.right / 2);
cy = r.top + (cr.bottom / 2);

// Move mouse to centre of client
SetCursorPos(cx, cy);
double maxRadius = min(cr.right  / 2, cr.bottom / 2);

double angle = 0;

double to_rad = PI / 180; // Multiplyer to convert to radians

// convert angle speed to radians

// Left mouse button down
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);

// Loop
SetCursorPos(
cx + int(cos(angle) * radius), // X
cy + int(sin(angle) * radius)  // Y
);
angle += angleSpeed;
Sleep(interval);
}

// Left mouse button up
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

// Pause
cin.ignore();
return 0;
}``````
1
Contributor
0
Replies
2
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.