I’m drawing a series of lines that diverge in the middle of the screen using a gaussian function (getGaussian below). Usually this is fine as you can see in theis picture (only two lines here to see the issue.
The problem is that when I use curveVertex to draw it, I sometimes get a weird line piece at the beginning and the end. This seems to happen if the curve is shallow and when there is a big jump from the previous y value to the first of the curve section.
Using plain vertex just causes a straight line jutting up. I have tried adding points in between so the jump is less dramatic but a similar appearence happens.
I’m really stuck on how to make this have a smooth transition. Part of it is the the getGaussian function just returns a high y value causing that jump. Is there a way to ensure the function returns a better y value or a way to scale all the y values to prevent this? or is there some other aesthetic hack i can make with curveVertex?
here’s the full code so you can see the parts:
int windWidth = 1000;
int widthMargin = 150;
int windHeight = 600;
int heightMargin = 50;
int numLines = 2;
int numPts = 200;
float incX = (windWidth - (widthMargin*2)) / float(numPts);
PVector stdRange = new PVector(1.0, 5.0);
void setup(){
size(1000, 600, P3D);
background(255);
smooth(8);
noLoop();
}
void draw() {
background(255);
for(int i=0; i<numLines; i++){
float mean = random(-1.25, 1.25);
float stdd = random(stdRange.x, stdRange.y);
float yOff = random(4);
float yStart = 0.0;
float yDir = random(0,1) < 0.5 ? -1:1;
noFill();
beginShape();
strokeWeight(2);
stroke(50);
curveVertex(0, height/2);
curveVertex(0, height/2);
for(int j=0; j<width-(widthMargin*2); j++){
float xPos = widthMargin + j;
float xVal = map(xPos, widthMargin, width-widthMargin, -stdRange.y, stdRange.y);
float yVal = getGaussian(xVal, mean, stdd);
float yPos = 0.0;
if(i%2 == 0){
yStart = height/2 - yOff;
yPos = map(yVal, 0, 1, yStart, 0+heightMargin);
} else {
yStart = height/2 + yOff;
yPos = map(yVal, 0, 1, yStart, height-heightMargin);
}
if(j == 0){
curveVertex(widthMargin-1, yStart);
curveVertex(widthMargin, yPos);
} else if(j == (width-(widthMargin*2))-1){
curveVertex(width-widthMargin-1, yPos);
curveVertex(width-widthMargin, yStart);
} else{
curveVertex(xPos, yPos);
//println(xPos + " -- " + yPos);
}
}
curveVertex(width, height/2);
curveVertex(width, height/2);
endShape();
}
}
float getGaussian(float x, float mean, float variance) {
return (1 / sqrt(TWO_PI * variance)) * exp(-sq(x - mean) / (2 * variance));
}