Hallo,
Ich bin momentan dabei, für ein Uni Projekt den Diamond-Square-Algorithmus zu implementieren. Es ist schon ziemlich viel vorgegeben gewesen. es geht darum, dass ich einen eindimensionalen vector erzeugen soll, der zu einer bestimmten resolution, die in der build command line angegeben wird, das height_field ausgeben soll. height_min ist 0 und height_max ist 1
mein code wie er momentan ist liefert mir jedoch beim kompilieren ständig "vector subscript out of bounds". woran kann das liegen. da ich relativ neu zu c++ bin ist mein code sicher fehlerhaft.
bitte um hilfe
LG
Spacemoose
Ich bin momentan dabei, für ein Uni Projekt den Diamond-Square-Algorithmus zu implementieren. Es ist schon ziemlich viel vorgegeben gewesen. es geht darum, dass ich einen eindimensionalen vector erzeugen soll, der zu einer bestimmten resolution, die in der build command line angegeben wird, das height_field ausgeben soll. height_min ist 0 und height_max ist 1
mein code wie er momentan ist liefert mir jedoch beim kompilieren ständig "vector subscript out of bounds". woran kann das liegen. da ich relativ neu zu c++ bin ist mein code sicher fehlerhaft.
bitte um hilfe

LG
Spacemoose
Code:
#include "DiamondSquare.h"
#include <stdlib.h>
#include <time.h>
#include <vector>
using namespace std;
#include <iostream>
unsigned int i;
DiamondSquare::DiamondSquare(int resolution, float height_min, float height_max)
: m_resolution(resolution), m_height_min(height_min), m_height_max(height_max)
{
float default_height = m_height_min;
m_heightfield.resize(resolution*resolution, default_height);
}
DiamondSquare::~DiamondSquare(void)
{
}
float DiamondSquare::GetHeight(int x, int y)
{
return m_heightfield[y*m_resolution+x];
}
uint16_t DiamondSquare::GetHeightAsUint(int x, int y)
{
float height = GetHeight(x,y);
//normalize height to [0;1]
float height_normalized = (height - m_height_min) / (m_height_max - m_height_min);
//convert to integer and return height
return (uint16_t)(height_normalized * (float)UINT16_MAX);
}
// random number between -1 and 1
float rnd() {
float r = 2.0f * ((float)rand() / (float)RAND_MAX) - 1.0f;
return r;
}
void DiamondSquare::SetHeight(int x, int y, float value)
{
m_heightfield[y*m_resolution+x] = value;
}
void DiamondSquare::GenerateHeightfield()
{
srand((unsigned)time(0));
double roughness = 1.0;
int size = m_resolution*m_resolution;
int initialStepsize = m_resolution;
int stepsize=0;
float x = 0;
float y = 0;
//initialise with random numbers
for(float i = 1; i<size; i++){
m_heightfield.at(i)=0;
}
//diamond-square-algorithm
//TODO
}
void DiamondSquare::SquareStep(float x, float y, int size, double value){
int halfstep = size / 2;
//square
// a b
//
// x
//
// c d
uint16_t a = GetHeight(x - halfstep, y - halfstep);
uint16_t b = GetHeight(x + halfstep, y - halfstep);
uint16_t c = GetHeight(x - halfstep, y + halfstep);
uint16_t d = GetHeight(x + halfstep, y + halfstep);
SetHeight(x, y, ((a + b + c + d) / 4.0) + value);
}
void DiamondSquare::DiamondStep(float x, float y, int size, double value){
int halfstep = size / 2;
//diamond
// c
//
//a x b
//
// d
uint16_t a = GetHeight(x - halfstep, y);
uint16_t b = GetHeight(x + halfstep, y);
uint16_t c = GetHeight(x, y - halfstep);
uint16_t d = GetHeight(x, y + halfstep);
SetHeight(x, y, ((a + b + c + d) / 4.0) + value);
}
Zuletzt bearbeitet: