Skip to main content
added 69 characters in body
Source Link
Sidar
  • 3.6k
  • 2
  • 17
  • 28

Not exactly what I'm looking for but I've opt it for another less "elegant" way of doing it.

I set up a table of points and their values.

The object looks like

public class CustomCurve {
    
    public float[] points; 
    public float[] height;
    public int size;
    
    public CustomCurve(int size){
        points = new float[size];
        height = new float[size];
        this.size = size;
    }   
}

points are the points on line t;
height are their values from which the lines are drawn from one point to another.

The function that goes with it:

 public static float Custom(float t, CustomCurve c)
 {
    
    float result = 0.0f;

    
    if(t > 1) return c.height[c.height.Length-1];
    if(t < 0) return c.height[0];
    
    for (int i = 0; i < c.size-1; i++) {
        if(t >= c.points[i] && t <= c.points[i+1]){

            result = Linear(t/(c.points[i+1]-c.points[i]),c.height[i],c.height[i+1]);
            break;
        }
        
    }
    return result;
    
}

Simply put, I iterate over the points on my t line. Check if t is between any of the points and calculate the inner interpolation of two points. Using the simple linear interpolation equation:

v0+(v1-v0)*t;

So a table of:

Point   Height
0       0
0,2     0,95
0,3     1
0,33    0,95
0,4     0
0,5     0,95
0,7     1
0,8     0,95
1       0

Looks like: enter image description hereenter image description here

Granted, the line won't be smooth. But if I need more detail I add more points. And once created I can reuse them.

If anyone knows a way to get rid of the forloop please do tell!

Not exactly what I'm looking for but I've opt it for another less "elegant" way of doing it.

I set up a table of points and their values.

The object looks like

public class CustomCurve {
    
    public float[] points; 
    public float[] height;
    public int size;
    
    public CustomCurve(int size){
        points = new float[size];
        height = new float[size];
        this.size = size;
    }   
}

points are the points on line t;
height are their values from which the lines are drawn from one point to another.

The function that goes with it:

 public static float Custom(float t, CustomCurve c)
 {
    
    float result = 0.0f;

    
    if(t > 1) return c.height[c.height.Length-1];
    if(t < 0) return c.height[0];
    
    for (int i = 0; i < c.size-1; i++) {
        if(t >= c.points[i] && t <= c.points[i+1]){

            result = Linear(t/(c.points[i+1]-c.points[i]),c.height[i],c.height[i+1]);
            break;
        }
        
    }
    return result;
    
}

Simply put, I iterate over the points on my t line. Check if t is between any of the points and calculate the inner interpolation of two points. Using the simple linear interpolation equation:

v0+(v1-v0)*t;

So a table of:

Point   Height
0       0
0,2     0,95
0,3     1
0,33    0,95
0,4     0
0,5     0,95
0,7     1
0,8     0,95
1       0

Looks like: enter image description here

Granted, the line won't be smooth. But if I need more detail I add more points. And once created I can reuse them.

Not exactly what I'm looking for but I've opt it for another less "elegant" way of doing it.

I set up a table of points and their values.

The object looks like

public class CustomCurve {
    
    public float[] points; 
    public float[] height;
    public int size;
    
    public CustomCurve(int size){
        points = new float[size];
        height = new float[size];
        this.size = size;
    }   
}

points are the points on line t;
height are their values from which the lines are drawn from one point to another.

The function that goes with it:

 public static float Custom(float t, CustomCurve c)
 {
    
    float result = 0.0f;

    
    if(t > 1) return c.height[c.height.Length-1];
    if(t < 0) return c.height[0];
    
    for (int i = 0; i < c.size-1; i++) {
        if(t >= c.points[i] && t <= c.points[i+1]){

            result = Linear(t/(c.points[i+1]-c.points[i]),c.height[i],c.height[i+1]);
            break;
        }
        
    }
    return result;
    
}

Simply put, I iterate over the points on my t line. Check if t is between any of the points and calculate the inner interpolation of two points. Using the simple linear interpolation equation:

v0+(v1-v0)*t;

So a table of:

Point   Height
0       0
0,2     0,95
0,3     1
0,33    0,95
0,4     0
0,5     0,95
0,7     1
0,8     0,95
1       0

Looks like: enter image description here

Granted, the line won't be smooth. But if I need more detail I add more points. And once created I can reuse them.

If anyone knows a way to get rid of the forloop please do tell!

Source Link
Sidar
  • 3.6k
  • 2
  • 17
  • 28

Not exactly what I'm looking for but I've opt it for another less "elegant" way of doing it.

I set up a table of points and their values.

The object looks like

public class CustomCurve {
    
    public float[] points; 
    public float[] height;
    public int size;
    
    public CustomCurve(int size){
        points = new float[size];
        height = new float[size];
        this.size = size;
    }   
}

points are the points on line t;
height are their values from which the lines are drawn from one point to another.

The function that goes with it:

 public static float Custom(float t, CustomCurve c)
 {
    
    float result = 0.0f;

    
    if(t > 1) return c.height[c.height.Length-1];
    if(t < 0) return c.height[0];
    
    for (int i = 0; i < c.size-1; i++) {
        if(t >= c.points[i] && t <= c.points[i+1]){

            result = Linear(t/(c.points[i+1]-c.points[i]),c.height[i],c.height[i+1]);
            break;
        }
        
    }
    return result;
    
}

Simply put, I iterate over the points on my t line. Check if t is between any of the points and calculate the inner interpolation of two points. Using the simple linear interpolation equation:

v0+(v1-v0)*t;

So a table of:

Point   Height
0       0
0,2     0,95
0,3     1
0,33    0,95
0,4     0
0,5     0,95
0,7     1
0,8     0,95
1       0

Looks like: enter image description here

Granted, the line won't be smooth. But if I need more detail I add more points. And once created I can reuse them.