Spirals and shapes

William Hemsworth 0 Tallied Votes 164 Views Share

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
Radius Speed: 1
Frame interval: 15

Box
Angle Speed: 90
Radius Speed: 1
Frame interval: 15

Curved Box
Angle Speed: 91
Radius Speed: 1
Frame interval: 15

Triangular
Angle Speed: 120
Radius Speed: 2
Frame interval: 15

Triangular curved
Angle Speed: 121
Radius Speed: 2
Frame interval: 15

Random
Angle Speed: 156
Radius Speed: 1
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;
	double radiusSpeed;
	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> ";
	cin >> radiusSpeed;

	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);

	// Let mspaint load
	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);

	// Ready to draw
	double radius = 0; // Radius from centre
	double angle = 0;

	double to_rad = PI / 180; // Multiplyer to convert to radians
	
	// convert angle speed to radians
	angleSpeed *= to_rad;

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

	// Loop
	for (; radius < maxRadius; radius += radiusSpeed) {
		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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.