00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#include <stdincl.h>
00024
#include <math.h>
00025
00026
#include "client.h"
00027
#include "mandel.h"
00028
#include "Globals.h"
00029
#include "events/Facing.h"
00030
#include "events/Position.h"
00031
#include "Grid.h"
00032
00033
00034 Grid::Grid()
00035 :m_x(640)
00036 ,m_y(480)
00037 ,m_base(40)
00038 ,m_hbase( (int)(cos(2 * M_PI * 30 / 360) * (double)m_base) )
00039 ,m_left(0)
00040 ,m_right(0)
00041 ,m_top(0)
00042 ,m_bottom(0)
00043 ,m_w(0)
00044 ,m_h(0)
00045 ,m_riktning(-1)
00046 ,m_xpos(-1)
00047 ,m_ypos(-1)
00048 {
00049
ReCalculate();
00050
for (
double d = 0; d < 360; d++)
00051 {
00052
m_sin[(
int)d] = sin(2 * M_PI * d / 360);
00053 }
00054 }
00055
00056
00057 Grid::~Grid()
00058 {
00059 }
00060
00061
00062 void Grid::ReCalculate()
00063 {
00064
double hstep = cos(2 * M_PI * 30 / 360);
00065
00066
DEB(
00067
for (
double h = 0; h <= 180; h += 45)
00068 {
00069 printf(
"cos(%.0f) = %f\n", h, cos(2 * M_PI * h / 360));
00070 printf(
"sin(%.0f) = %f\n", h, sin(2 * M_PI * h / 360));
00071 })
00072
m_hbase = (
int)(hstep * (
double)
m_base);
00073
00074
m_w = ((
m_x -
m_base/2) / 2) /
m_base;
00075
m_w =
m_w * 2 + 1;
00076
00077
m_h = (
m_y / 2) /
m_hbase;
00078
m_h =
m_h * 2 + 1;
00079
00080
while (
m_x / 2 +
m_left *
m_base >= 0)
00081 {
00082
m_left--;
00083 }
00084
while (
m_x / 2 +
m_right * m_base <
m_x)
00085 {
00086
m_right++;
00087 }
00088
#define MAX_ELEVATION 255
00089
while (
m_y / 2 +
m_top *
m_hbase >= 0 -
MAX_ELEVATION)
00090 {
00091
m_top--;
00092 }
00093
while (
m_y / 2 +
m_bottom * m_hbase <
m_y +
MAX_ELEVATION)
00094 {
00095
m_bottom++;
00096 }
00097
DEB(
00098 printf(
"Resolution %d x %d with a base of %d gives\n",
m_x,
m_y,m_base);
00099 printf(
" base = %d\n",m_hbase);
00100 printf(
" width = %d\n",m_w);
00101 printf(
" height = %d\n",m_h);
00102 printf(
" X %d..%d\n",
m_left,
m_right);
00103 printf(
" Y %d..%d\n",
m_top,
m_bottom);)
00104 }
00105
00106
00107 void Grid::SetSize(
int x,
int y)
00108 {
00109
m_x = x;
00110
m_y = y;
00111
ReCalculate();
00112 }
00113
00114
00115 void Grid::SetBase(
int b)
00116 {
00117
m_base = b;
00118
ReCalculate();
00119 }
00120
00121
00122 int Grid::GetBase()
00123 {
00124
return m_base;
00125 }
00126
00127
00128 int Grid::GetWidth()
00129 {
00130
return m_w;
00131 }
00132
00133
00134 int Grid::GetHeight()
00135 {
00136
return m_h;
00137 }
00138
00139
00140 void Grid::GetXY(
int x,
int y,
int *cx,
int *cy,
int *elevation)
00141 {
00142
00143
00144
00145
00146
00147
00148
00149
00150 *cx =
m_x / 2 + x *
m_base;
00151
if (y % 2)
00152 {
00153 *cx = *cx + m_base / 2;
00154 }
00155 *cy =
m_y / 2 + y *
m_hbase;
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 *elevation =
mandel_color(x,y);
00175 }
00176
00177
00178 int Grid::GetHBase()
00179 {
00180
return m_hbase;
00181 }
00182
00183
00184 double Grid::Sin(
int x)
00185 {
00186
return m_sin[x % 360];
00187 }
00188
00189
00190 void Grid::SetDirection(Event::Facing *e)
00191 {
00192
m_riktning = e -> GetRiktning();
00193 }
00194
00195
00196 void Grid::SetPosition(Event::Position *e)
00197 {
00198
m_xpos = e -> GetX();
00199
m_ypos = e -> GetY();
00200 }
00201