
TryItOnline will open in another window where you can interact with the code.
Link:
​
Two random number generation functions simplify the interface with standard library implementation:
​
#include <iostream>
#include <random>
#include <ctime>
#include <vector>
float Random_scalar(float mean, float stddev);
int Random_range(int Low, int High);
​
int main(){
std::cout << Random_scalar(0.5f, 1.0f) << std::endl;
std::cout << Random_range(-4, 5) << std::endl;
return 0;}
​
float Random_scalar(float mean, float stddev) {
if (stddev == 0.0f) {stddev = 0.0001f;}
std::random_device rd;
std::mt19937 generator(rd());
std::normal_distribution<float> dist(mean, stddev);
return dist(generator);
}
int Random_range(int Low, int High){
std::vector<float> RandArray;
int HighElement = 0;
float HighVal = -40;
std::random_device rd;
std::mt19937 generator(rd());
std::normal_distribution<float> dist(0.3f, 0.6f);
RandArray.resize( (abs(High - Low) + 1), 0);
for (int i = 0; i < RandArray.size(); i++){
RandArray[i] = dist(generator);
}
for (int i = 0; i < RandArray.size(); i++){
if (RandArray[i] > HighVal){ HighVal = RandArray[i]; HighElement = i; }
}
HighElement = HighElement + Low;
return HighElement;
}
Link:
​
Generates an image pattern for a given vector. The example is best in combination with a viewer:
​
#include <vector>
#include <math.h>
#include <iostream>
void PatternGen(std::vector<std::vector<std::vector<float> > >& Texture, int Type, float Slider) {
if (Slider == 0.0f) { Slider = 0.00001f; }
float RGBT[4] = { 0 };
RGBT[3] = 1.0f;
RGBT[0] = fmax(Slider, 0);
RGBT[1] = 1 - fabs(Slider);
RGBT[2] = fmax( fmin( (Slider * -1), 1 ), 0);
int LineRadius = Texture[0].size() / 12;
int LineX = Texture[0].size() * ((Slider + 1) / 2);
int LineY = Texture.size() * ((Slider + 1) / 2);
int XDis;
int YDis;
float CenterDis;
float XCenter = Texture[0].size() / 2.0f;
float YCenter = Texture.size() / 2.0f;
int SlideSign = (-2 * (signbit(Slider))) + 1;
float SlideInScreen = ((Slider + 1) / 2) * Texture[0].size();
if (SlideInScreen == 0.0f) { SlideInScreen = 0.00001f; }
int CheckerSize = SlideInScreen;
int Xon = 0;
int Yon = 0;
int XCount = 0;
int YCount = 0;
for (int y = 0; y < Texture.size(); y++){
YCount = YCount + 1;
XCount = 0;
Xon = 0;
YDis = abs(LineY - y);
for (int x = 0; x < Texture[y].size(); x++){
float Xin = (float)x;
float Yin = (float)y;
XCount = XCount + 1;
CenterDis = sqrt(pow((x - XCenter), 2) + pow((y - YCenter), 2));
XDis = abs(LineX - x);
for (int channel = 0; channel < Texture[y][x].size(); channel++){
if (Type == 0) { Texture[y][x][channel] = RGBT[channel]; }//Color
if (Type ==1) { if (XDis < LineRadius) { Texture[y][x][channel] = 1; } }//Vertical Bar
if (Type == 2) { if (YDis < LineRadius) { Texture[y][x][channel] = 1; } }//Horizontal Bar
if (Type == 3) { Texture[y][x][channel] = (Xin / (Texture[0].size())); }// Vertical Gradient
if (Type == 4) { Texture[y][x][channel] = (Yin / (Texture.size())); }// Horizontal Gradient
if (Type ==5) { Texture[y][x][channel] = 1 - (fmin(fmax(((CenterDis / Texture[0].size()) / ((Slider + 1.0f) / 2.0f)), 0.0f), 1.0f)); }//Gradient Circle
if (Type == 6) { if ((CenterDis / Texture[0].size()) < ((Slider + 1.0f) / 4.0f)) { Texture[y][x][channel] = 1; } }//Circle
if (Type == 7) {Texture[y][x][channel] = 0; }// Square
if (Type == 8) {Texture[y][x][channel] = 0; }// Triangle
if (Type ==9) {Texture[y][x][channel] = fabs(Yon - Xon); }
}
if (XCount >= CheckerSize){
XCount = 0;
if (Xon == 0) { Xon = 1; }
else { Xon = 0; }
}
}
if (YCount >= CheckerSize){
YCount = 0;
if (Yon == 0) { Yon = 1; }
else { Yon = 0; }
}
}
}
int main(){
std::vector<std::vector<std::vector<float> > > Image;
Image.resize(16);
for (int i = 0; i < Image.size(); i++){
Image[i].resize(16);
for (int j = 0; j < Image[i].size(); j++){
Image[i][j].resize(4, 0);
}
}
PatternGen(Image, 0, -0.9f);
std::cout << Image[13][3][0] << std::endl;
return 0;
}