Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extrudeWithRadius rounds top and bottom differently #45

Open
varalgit opened this issue Jan 5, 2023 · 3 comments
Open

extrudeWithRadius rounds top and bottom differently #45

varalgit opened this issue Jan 5, 2023 · 3 comments

Comments

@varalgit
Copy link

varalgit commented Jan 5, 2023

If you specify the same radius for top and bottom, the rendered shape is different for the top and the bottom. The top and bottom layers seem to be shifted by 1 layer. Example (see picture):

extrudeWithRadius(5,r1=2,r2=2,fn=10)
  offset(r = 5) 
    offset(delta = -5)  
      polygon([[0,0],[20,0],[20,20]]);


module extrudeWithRadius(length,r1=0,r2=0,fn=30){
  n1=sign(r1);n2=sign(r2);
  r1=abs(r1);r2=abs(r2);
 # translate([0,0,r1]){
    linear_extrude(length-r1-r2){
      children();
    }
  }
  for(i=[0:fn-1]){
    translate([0,0,i/fn*r1]){
      linear_extrude(r1/fn+0.01){
        offset(n1*sqrt(sq(r1)-sq(r1-i/fn*r1))-n1*r1){
          children();
        }
      }
    }
    translate([0,0,length-r2+i/fn*r2]){
      linear_extrude(r2/fn+0.01){
        offset(n2*sqrt(sq(r2)-sq(i/fn*r2))-n2*r2){
          children();
        }
      }
    }
  }

}

function sq(x)=x*x;

extrudeWithRadius-rounds-top-and-bottom-differently

@varalgit
Copy link
Author

varalgit commented Jan 5, 2023

And it can be fixed with the change of one line:

offset(n2*sqrt(sq(r2)-sq(i/fn*r2))-n2*r2){

should be:

offset(n2*sqrt(sq(r2)-sq((i+1)/fn*r2))-n2*r2){

Example (I added coloring so you can see the layers):

extrudeWithRadius(5,r1=2,r2=2,fn=10)
  offset(r = 5) 
    offset(delta = -5)  
      polygon([[0,0],[20,0],[20,20]]);


module extrudeWithRadius(length,r1=0,r2=0,fn=30){
  n1=sign(r1);n2=sign(r2);
  r1=abs(r1);r2=abs(r2);
 # translate([0,0,r1]){
    linear_extrude(length-r1-r2){
      children();
    }
  }
  for(i=[0:fn-1]){
    color(c=[0,(i+1)/fn,0])
    translate([0,0,i/fn*r1]){
      linear_extrude(r1/fn+0.01){
        offset(n1*sqrt(sq(r1)-sq(r1-i/fn*r1))-n1*r1){
          children();
        }
      }
    }
    color(c=[0,(fn-i)/fn,0])
    translate([0,0,length-r2+i/fn*r2]){
      linear_extrude(r2/fn+0.01){
        offset(n2*sqrt(sq(r2)-sq((i+1)/fn*r2))-n2*r2){
          children();
        }
      }
    }
  }
}

function sq(x)=x*x;

extrudeWithRadius-rounds-top-and-bottom-differently (fixed)

@varalgit varalgit closed this as completed Jan 5, 2023
@varalgit varalgit reopened this Jan 5, 2023
@varalgit
Copy link
Author

varalgit commented Jan 5, 2023

I closed this issue, but the code has not changed so it probably should stay open. Sorry about this.

@varalgit
Copy link
Author

varalgit commented Jan 6, 2023

You can also change it the other way round, which I like better:

$fn=72;

extrudeWithRadius(5,r1=2,r2=2,fn=10)
  offset(r = 5) 
    offset(delta = -5)  
      polygon([[0,0],[20,0],[20,20]]);


module extrudeWithRadius(length,r1=0,r2=0,fn=30){
  n1=sign(r1);n2=sign(r2);
  r1=abs(r1);r2=abs(r2);
 # translate([0,0,r1]){
    linear_extrude(length-r1-r2){
      children();
    }
  }
  for(i=[0:fn-1]){
    color(c=[0,(i+1)/fn,0])
    translate([0,0,i/fn*r1]){
      linear_extrude(r1/fn+0.01){
        offset(n1*sqrt(sq(r1)-sq(r1-(i+1)/fn*r1))-n1*r1){
          children();
        }
      }
    }
    color(c=[0,(fn-i)/fn,0])
    translate([0,0,length-r2+i/fn*r2]){
      linear_extrude(r2/fn+0.01){
        offset(n2*sqrt(sq(r2)-sq(i/fn*r2))-n2*r2){
          children();
        }
      }
    }
  }
}

function sq(x)=x*x;

extrudeWithRadius-rounds-top-and-bottom-differently-(fixed-better)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant