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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*
translation.cpp
translates a circle in 2D using matrix multiply
*/
#include <math.h>
#include <stdio.h>
#include <windows.h>
#include "resource.h"
// your path for this include may vary
#include "GraphicsFramework.h"
// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;
HWND HOutput = 0; // handle to the output control
HWND HDialog = 0;
const double PI = 3.1415927;
double rad = 1.0471975511966;
// 2d point or vector
struct Vector2 {
double x, y; // x, y coordinate
double w; // homogeneous coordinate, usually = 1
// default constructor set to <0, 0, 1>
Vector2() {
x = 0.0;
y = 0.0;
w = 1.0;
}
};
// 2d matrix 3x3
struct Matrix3 {
double m[3][3]; // 3x3 matrix values
double rm[3][3]; // 3x3 matrix values
// default constructor - initialize matrix to identity
Matrix3() {
m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = 0.0;
m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = 0.0;
m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0;
}
// make a 2D translation matrix
void makeTranslationMatrix(double dx, double dy) {
m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = dx;
m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = dy;
m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0;
}
void makeRotationMatrix() {
rm[0][0] = 0.0; rm[0][1] = 1.0; rm[0][2] = 0.0;
rm[1][0] = -1.0; rm[1][1] = 0.0; rm[1][2] = 0.0;
rm[2][0] = 0.0; rm[2][1] = 0.0; rm[2][2] = 1.0;
}
};
// mulitplies matrix m by vector v, returns result = m x v
Vector2 Multiply(Matrix3 m, Vector2 v) {
Vector2 result;
result.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.w;
result.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.w;
result.w = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.w;
return result;
}
void Circle(int xCenter, int yCenter, int r, unsigned int color) {
double d;
int rSquared;
int x, y;
rSquared = r * r;
x = 0;
y = r;
d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
while (y >= x) {
PGraphics->AddPoint(xCenter + x, yCenter + y, color);
PGraphics->AddPoint(xCenter - x, yCenter + y, color);
PGraphics->AddPoint(xCenter + x, yCenter - y, color);
PGraphics->AddPoint(xCenter - x, yCenter - y, color);
PGraphics->AddPoint(xCenter + y, yCenter + x, color);
PGraphics->AddPoint(xCenter - y, yCenter + x, color);
PGraphics->AddPoint(xCenter + y, yCenter - x, color);
PGraphics->AddPoint(xCenter - y, yCenter - x, color);
if (d > 0) {
}
else {
y--;
}
x++;
d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
}
}
void DrawStuff() {
COLORREF green = RGB(0, 255, 0); // green color to draw with
COLORREF purple = RGB(255, 0, 255); // purple color to draw with
Vector2 circleCenter; // center of circle
Vector2 translation; // translation amount
Matrix3 m; // matrix to store the translation
Matrix3 rm; // matrix to store the rotation
Vector2 newCircleCenter; // translated circle center
int radius = 20; // circle radius
double rad = 1.0472;
char str[32]; // string to store user input
// clear the scene and add an axis
PGraphics->ClearScene(RGB(0, 0, 0));
PGraphics->AddAxis(RGB(150, 150, 150), 10);
// get the user input from the edit boxes and
// convert string input to double
GetDlgItemText(HDialog, IDC_EDIT_CIRCLEX, str, 32);
circleCenter.x = atof(str);
GetDlgItemText(HDialog, IDC_EDIT_CIRCLEY, str, 32);
circleCenter.y = atof(str);
GetDlgItemText(HDialog, IDC_EDIT_TRANSLATIONX, str, 32);
translation.x = atof(str);
GetDlgItemText(HDialog, IDC_EDIT_TRANSLATIONY, str, 32);
translation.y = atof(str);
// set matrix m to the necessary translation matrix
m.makeTranslationMatrix(translation.x, translation.y);
rm.makeRotationMatrix();
// transform the circle center newCircleCenter = m x circleCenter
newCircleCenter = Multiply(m, circleCenter);
newCircleCenter = Multiply(rm, newCircleCenter);
// draw the original and transformed circles
Circle(circleCenter.x, circleCenter.y, radius, green);
Circle(newCircleCenter.x, newCircleCenter.y, radius, purple);
// draw the points
PGraphics->Draw();
}
/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
// dialog is initializing - store the picture box handle in a global variable for later
HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);
// instantiate and initialize our graphics framework object
PGraphics = new GraphicsFramework(HOutput);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN_DRAW:
// draw button was pressed
DrawStuff();
break;
case IDC_BTN_CLEAR:
// clear button was pressed so clear the scene and draw the empty scene
PGraphics->ClearScene(RGB(0, 0, 0));
PGraphics->Draw();
break;
case IDCANCEL:
// user is quitting so release the GraphicsFramework object and quit
delete PGraphics;
PostQuitMessage(0);
break;
}
}
return FALSE;
}
// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
// create the main window
// store its handle in a global if needed
HDialog = CreateDialog (GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_DIALOG1),
0,
DialogProc);
// make the dialog visible
ShowWindow(HDialog, SW_SHOW);
// standard windows message loop
MSG msg;
int status;
while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
{
if (status == -1)
return -1;
// avoid processing messages for the dialog
if (!IsDialogMessage (HDialog, & msg))
{
TranslateMessage ( & msg );
DispatchMessage ( & msg );
}
}
return (int)(msg.wParam);
}
|