Finding the Tangent at Each Point of a Randomized Slope
in
Programming Questions
•
4 months ago
So someone I knew wrote some code that I have adapted to processing to create a randomized slope based on what appears to be the first four terms of a randomized Fourier series (?). I've been writing a project where I want to know what the tangent line is at each point so that I can align a sprite correctly. Unfortunately, I haven't had too much success. Part of the problem is that I'm not sure if the derivative is correct, and the other is that I'm not entirely sure that I'm drawing the line correctly (regardless of whether the derivative is anywhere near what it should be.
Any pointers or assistance would be most appreciated. This is what I have so far:
- final int w = 800;
- final int h = 480;
- double[] skyline;
- PImage img;
- int numOfDeriv = 800;
- int derivModBy = 1; //Determines how many points will be checked
- int time;
- int timeDelay = 1000;
- int iter;
- double[] derivatives;
- void setup() {
- noStroke();
- size(w, h);
- fill(0,128,255);
- rect(0,0,w,h);
- int t[] = terrain(w,h);
- fill(77,0,0);
- for(int i=0; i < w; i++){
- rect(i, h, 1, -1*t[i]);
- }
- time = millis();
- timeDelay = 100;
- iter =0;
- img = get();
- }
- void draw() {
- int dnum = 0; //Current position of derivatives
- if(iter == numOfDeriv) iter = 0;
- if (millis() > time + timeDelay){
- image(img, 0, 0, width, height);
- strokeWeight(4);
- stroke(255,0,0);
- point((float)iter*derivModBy, height-(float)skyline[iter*derivModBy]);
- strokeWeight(1);
- stroke(255,255,0);
- print("At x = ");
- print(iter);
- print(", y = ");
- print(skyline[iter]);
- print(", derivative = ");
- print((float)derivatives[iter]);
- print('\n');
- lineAngle(iter, (int)(height-skyline[iter]), (float)derivatives[iter], 100);
- lineAngle(iter, (int)(height-skyline[iter]), (float)derivatives[iter], -100);
- stroke(126);
- time = millis();
- iter += 1;
- }
- }
- void lineAngle(int x, int y, float angle, float length)
- {
- line(x, y, x+cos(angle)*length, y-sin(angle)*length);
- }
- int[] terrain(int w, int h){
- width = w;
- height = h;
- //min and max bracket the freq's of the sin/cos series
- //The higher the max the hillier the environment
- int min = 1, max = 6;
- //allocating horizon for screen width
- int[] horizon = new int[width];
- skyline = new double[width];
- derivatives = new double[numOfDeriv];
- //ratio of amplitude of screen height to landscape variation
- double r = (int) 2.0/5.0;
- //number of terms to be used in sine/cosine series
- int n = 4;
- int[] f = new int[n*2];
- //calculating omegas for sine series
- for(int i = 0; i < n*2 ; i ++){
- f[i] = (int) random(max - min + 1) + min;
- }
- //amp is the amplitude of the series
- int amp = (int) (r*height);
- int dnum = 0; //Current number of derivatives
- for(int i = 0 ; i < width; i ++){
- skyline[i] = 0;
- double derivative = 0.0;
- for(int j = 0; j < n; j++){
- if(i % derivModBy == 0){
- derivative += ( cos( (f[j]*PI*i/height) * f[j]*PI/height) -
- sin(f[j+n]*PI*i/height) * f[j+n]*PI/height);
- }
- skyline[i] += ( sin( (f[j]*PI*i/height) ) + cos(f[j+n]*PI*i/height) );
- }
- skyline[i] *= amp/(n*2);
- skyline[i] += (height/2);
- skyline[i] = (int)skyline[i];
- horizon[i] = (int)skyline[i];
- derivative *= amp/(n*2);
- if(i % derivModBy == 0){
- derivatives[dnum++] = derivative;
- derivative = 0;
- }
- }
- return horizon;
- }
- void reset() {
- time = millis();
- }
1