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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "sketch.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
#define PI 3.1415926536
#define WIDTH 700
#define HEIGHT 600
#define MENUWIDTH 100
#define BOXHEIGHT (HEIGHT/NCOLORS)
#define DSLEFT MENUWIDTH
#define DSRIGHT WIDTH
#define DSBOTTOM 0
#define DSTOP HEIGHT
#define NCOLORS 8
#define RGBRED 1, 0, 0
#define RGBGREEN 0, 1, 0
#define RGBBLUE 0, 0, 1
#define RGBYELLOW 1, 1, 0
#define RGBMAGENTA 1, 0, 1
#define RGBCYAN 0, 1, 1
#define RGBBLACK 0, 0, 0
#define RGBWHITE 1, 1, 1
#define R 0
#define G 1
#define B 2
Sketch sketcher;
static float colormenu[8][3] = {{RGBRED}, {RGBGREEN}, {RGBBLUE}, {RGBYELLOW}, {RGBMAGENTA}, {RGBCYAN}, {RGBWHITE}, {RGBBLACK}};
//Line
void Sketch::CreateObject(int c, int t, int x1, y1)
{
objects[n].colortype=c;
objects[n].tooltype=t;
objects[n].tool.endpoints.pt1.x=x1;
objects[n].tool.endpoints.pt1.y=y1;
objects[n].tool.endpoints.pt2.x=x1;
objects[n].tool.endpoints.pt2.y=y1;
n++;
}
//Rectangle
void Sketch::CreateObject(int c, int t, int x1, int y1, int x2, int y2)
{
objects[n].colortype=c;
objects[n].tooltype=t;
objects[n].tool.endpoints.pt1.x=x1;
objects[n].tool.endpoints.pt1.y=y1;
objects[n].tool.endpoints.pt2.x=x2;
objects[n].tool.endpoints.pt2.y=y2;
n++;
}
//Circle
void Sketch::CreateObject(int c, int t, int r, int x1, int y1)
{
objects[n].colortype=c;
objects[n].tooltype=t;
objects[n].tool.circle.radius=r;
objects[n].tool.circle.center.x=x1;
objects[n].tool.circle.center.y=y1;
n++;
}
void Sketch::UpdateObject(int _x, int _y)
{
switch(objects[n-1].toolType)
{
case 0:
case 1:
case 2:
case 3:
int x1=objects[n-1].tool.circle.center.x;
int y1=objects[n-1].tool.circle.center.y;
int x2=_x;
int y2=_y;
int r=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
objects[n-1].tool.circle.radius=r;
cout<<"Circle is updated"<<endl;
break;
case 4:
objects[n-1].tool.endpoints.pt2.x=_x;
objects[n-1].tool.endpoints.pt2.y=_y;
cout<<"Line/Rect is updated"<<endl;
break;
}
}
void Sketch::Load()
{
string filename;
ifstream fin;
cout<<"Load: ";
cin>>filename;
fin.open(filename.c_str());
if (fin.is_open())
{
while (fin.good())
{
fin>>n;
for(int i=0; i<n; i++)
{
fin>>objects[i].colortype;
fin>>objects[i].tooltype;
if(objects[i].tooltype==2 || objects[i].tooltype==3)
{
fin>>objects[i].tool.circle.center.x;
fin>>objects[i].tool.circle.center.y;
fin>>objects[i].tool.circle.radius;
}
else
{
fin>>objects[i].tool.endpoints.pt1.x;
fin>>objects[i].tool.endpoints.pt1.y;
fin>>objects[i].tool.endpoints.pt2.x;
fin>>objects[i].tool.endpoints.pt2.y;
}
}
}
}
else{cout<<"Load failed";}
}
void Sketch::Save()
{
string filename;
ofstream fout;
cout<<"Save as: ";
cin>>filename;
fout.open(filename.c_str());
if(fout.is_open())
{
fout<<n<<" ";
for(int i=0; i<n; i++)
{
fout<<objects[i].colortype<<" "<<objects[i].tooltype<<" ";
if(objects[i].tooltype==2 || objects[i].tooltype==3)
{
fout<<objects[i].tool.circle.center.x<<" ";
fout<<objects[i].tool.circle.center.y<<" ";
fout<<objects[i].tool.circle.radius<<" ";
}
else
{
fout<<objects[i].tool.endpoints.pt1.x<<" ";
fout<<objects[i].tool.endpoints.pt1.y<<" ";
fout<<objects[i].tool.endpoints.pt2.x<<" ";
fout<<objects[i].tool.endpoints.pt2.y<<" ";
}
fout<<endl;
}
}
}
void Sketch::Clear()
{
n=0;
glutPostRedisplay;
}
void Sketch::drawRectangle(int index, GLint type)
{
int c = objects[index].colortype;
int x1 = objects[index].tool.endpoints.pt1.x;
int y1 = objects[index].tool.endpoints.pt1.y;
int x2 = objects[index].tool.endpoints.pt2.x;
int y2 = objects[index].tool.endpoints.pt2.y;
glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][B]);
glBegin(type);
glVertex2i(x1,y1);
glVertex2i(x1,y2);
glVertex2i(x2,y2);
glVertex2i(x2,y1);
glEnd();
}
void Circle(GLint type)
{
int i=36;
float angle;
float anglecounter= 2*PI/i;
glBegin(type);
for(int k=0; k<i; k++)
{
angle = k*anglecounter;
glVertex2f(cos(angle),sin(angle));
}
glEnd();
}
void Sketch::drawCircle(int index, GLint type)
{
int c=objects[index].colortype;
int x=objects[index].tool.circle.center.x;
int y=objects[index].tool.circle.center.y;
float r=objects[index].tool.circle.radius;
glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][B]);
glPushMatrix();
glTranslatef(x,y,0);
glScalef(r,r,1);
Circle(type);
glPopMatrix();
}
void Sketch::drawLine(int index)
{
int c=objects[index].colortype;
int x1=objects[index].tool.endpoints.pt1.x;
int y1=objects[index].tool.endpoints.pt1.y;
int x2=objects[index].tool.endpoints.pt2.x;
int y2=objects[index].tool.endpoints.pt2.y;
glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][B]);
glBegin(GL_LINE_LOOP);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
}
void Sketch::draw()
{
for(int i=0; i<n; i++)
{
switch(objects[i].tooltype)
{
case 0: drawRectangle(i, GL_LINE_LOOP);
break;
case 1: drawRectangle(i, GL_POLYGON);
break;
case 2: drawCircle(i, GL_LINE_LOOP);
break;
case 3: drawCircle(i, GL_POLYGON);
break;
case 4: drawLine(i);
break;
case 5: drawLine(i);
break;
}
}
glutPostRedisplay();
}
|