-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphics.cpp
196 lines (169 loc) · 5.25 KB
/
graphics.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
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
#include "graphics.h"
#include "MyShape.h"
#include "algebra.h"
#include <Imagine/Graphics.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <sstream>
//#include <sstream>
using namespace std;
void ViewLayout::coordTranslate(double x,double y,int& x_onScreen,int& y_onScreen){
x_onScreen=(x-viewZoneLeft)*ratioWidth;
y_onScreen=(viewZoneUp-y)*ratioHeight;
}
void ViewLayout::coordTranslateInverse(int x_onScreen, int y_onScreen, double& realX,double& realY)
{
realX=x_onScreen/ratioWidth+viewZoneLeft;
realY=viewZoneUp-y_onScreen/ratioHeight;
}
GraphicManager::GraphicManager(int windowW,int windowH):
viewLayout(windowW,windowH)
{
frameCountPrintScreen=0;
}
void GraphicManager::coordTranslate(double x, double y, int &x_onScreen, int &y_onScreen)
{
viewLayout.coordTranslate(x,y,x_onScreen,y_onScreen);
}
void GraphicManager::coordTranslateInverse(int x_onScreen, int y_onScreen, double &realX, double &realY)
{
viewLayout.coordTranslateInverse(x_onScreen,y_onScreen,realX,realY);
}
void GraphicManager::myDisplay(std::vector<MyShape *> shapeList, Imagine::Color col)
{
Imagine::noRefreshPush();
Imagine::setBackGround(Imagine::WHITE);
drawAxis();
for(vector<MyShape*>::iterator i=shapeList.begin();i<shapeList.end();i++){
draw_each(*i,col,true);
}
Imagine::noRefreshPop();
}
void GraphicManager::draw_each(MyShape* s,Imagine::Color col,bool drawID)
{
switch (s->shapeType) {
case POLYGON:
myDrawPolygon(*static_cast<MyPolygon*>(s),col,drawID);
break;
case CIRCLE:
myDrawCircle(*static_cast<MyCircle*>(s),col,drawID);
break;
default:
break;
}
}
void GraphicManager::drawAxis()
{
int i,j;
int n=5;
for(i=-n;i<=n;i++){
myDrawLine(i,-n,i,n,Imagine::Color(200,200,200));
}
for(j=-n;j<=n;j++){
myDrawLine(-n,j,n,j,Imagine::Color(200,200,200));
}
myDrawLine(0,-n,0,n,Imagine::Color(200,200,200));
myDrawLine(-n,0,n,0,Imagine::Color(200,200,200));
}
void GraphicManager::myDrawPoint(const Vector2& p, int radius,Imagine::Color col) {
int x,y;
viewLayout.coordTranslate(p.x,p.y,x,y);
Imagine::fillCircle(x,y,radius,col);
}
void GraphicManager::myDrawPolygon(MyPolygon &p, Imagine::Color col,bool drawID){
assert(p.shapeType==POLYGON);
vector<Vector2 >::iterator v;
for (v=p.vertex.begin(); v<p.vertex.end()-1; ++v)
myDrawLine((*v).x,(*v).y,(*(v+1)).x,(*(v+1)).y, col);
myDrawLine(p.vertex.back().x,p.vertex.back().y,p.vertex.front().x,p.vertex.front().y, col);
if(drawID){
std::stringstream ss;
ss<<p.ID;
int xx1,yy1;
viewLayout.coordTranslate(p.center.x,p.center.y,xx1,yy1);
Imagine::drawString(xx1,yy1,(ss.str()),col);
}
}
void GraphicManager::myDrawCircle(MyCircle &p, Imagine::Color col,bool drawID){
if(p.shapeType!=CIRCLE){
cout<<"drawCircle need circle"<<endl;
throw logic_error("drawCircle need circle");
}
int xx1,yy1,xx2,yy2,r;
Vector2 pointOnCircle;
Transform trans(p.angle,p.center);
pointOnCircle=trans.Apply(Vector2(p.radius,0));
viewLayout.coordTranslate(p.center.x,p.center.y,xx1,yy1);
viewLayout.coordTranslate(pointOnCircle.x,pointOnCircle.y,xx2,yy2);
r=p.radius*viewLayout.oneMeterLengthOnScreen;
Imagine::drawCircle(xx1,yy1,r,col);
Imagine::drawLine(xx1,yy1,xx2,yy2,col);
if(drawID){
std::stringstream ss;
ss<<p.ID;
int xx1,yy1;
viewLayout.coordTranslate(p.center.x,p.center.y,xx1,yy1);
Imagine::drawString(xx1,yy1,(ss.str()),col);
}
}
void GraphicManager::myDrawLine(double x1,double y1,double x2,double y2, Imagine::Color col,int penWidth){
int xx1,xx2,yy1,yy2;
viewLayout.coordTranslate(x1,y1,xx1,yy1);
viewLayout.coordTranslate(x2,y2,xx2,yy2);
Imagine::drawLine(xx1,yy1,xx2,yy2,col,penWidth);
}
void GraphicManager::myNoRefreshPush()
{
Imagine::noRefreshPush();
}
void GraphicManager::myNoRefreshPop()
{
Imagine::noRefreshPop();
}
void GraphicManager::myMilliSleep(int milliSec)
{
Imagine::milliSleep(milliSec);
}
void GraphicManager::ajustViewZoom(Vector2 move, double zoom)
{
viewLayout.viewCenter+=move;
viewLayout.oneMeterLengthOnScreen*=zoom;
viewLayout.UpdateViewZone();
}
void GraphicManager::savePrintScreen()
{
byte *R,*G,*B;
int w,h;
char c[10];
stringstream ss;
ss<<frameCountPrintScreen;
Imagine::captureWindow(R,G,B,w,h);
std::string s("out");
s+=ss.str();
s+=".png";
Imagine::saveColorImage(s,R,G,B,w,h);
delete[] R;
delete[] G;
delete[] B;
frameCountPrintScreen++;
}
ViewLayout::ViewLayout(int windowWidth,int windowHeight)
{
oneMeterLengthOnScreen=25;
winWidth=windowWidth;
winHeight=windowHeight;
viewCenter=Vector2(0,0);
UpdateViewZone();
}
void ViewLayout::UpdateViewZone()
{
viewZoneLeft =-winWidth/2/oneMeterLengthOnScreen+viewCenter.x;
viewZoneRight =winWidth/2/oneMeterLengthOnScreen+viewCenter.x;
viewZoneUp =winHeight/2/oneMeterLengthOnScreen+viewCenter.y;
viewZoneBottom =-winHeight/2/oneMeterLengthOnScreen+viewCenter.y;
viewWidth=viewZoneRight-viewZoneLeft;
viewHeight=viewZoneUp-viewZoneBottom;
ratioWidth=winWidth/viewWidth;
ratioHeight=winHeight/viewHeight;
}