---
title: Extruding 2d Shapes
---

import Image from '@theme/IdealImage';

import extrude from '../../static/img/openscad-tutorial/extrude.png';
import differenceMarkup from '../../static/img/openscad-tutorial/difference-markup.png';
import extrude2 from '../../static/img/openscad-tutorial/extrude2.png';
import noFillet from '../../static/img/openscad-tutorial/no-fillet.png';

### Let get 3 dimensional

We've looked at this 2d shape long enough, we can give it some depth by extruding it, `linear_extrude`ing to be precise, let's add a new variable `hingeLength` too.

```cpp
// ... other variables above
hingeLength=30;
// highlight-next-line
linear_extrude(hingeLength){
  offset(1)offset(-2)offset(1){
    translate([0,pivotRadius,0]){
      circle(pivotRadius);
    }
    square([pivotRadius,pivotRadius]);
    translate([pivotRadius,0,0]){
      square([baseWidth,baseThickness]);
    }
  }
}
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJxVkM8KwjAMxu99ih48tDBhf/BU+gaeRPAgQ+qWbcXRadOJIHt355wju4Tw/ZJ8STaV01mq2N0+u3Awpe1R7xS7GoRjY4ubA0Sd/YSTLUOjkxGzxroa9uDqUchixVrrwPgLvILvSxAEyzfjvKsqhCASOSfbVC7Sl3MevHHYmgDiHEdkmSjO5wrOC+uLFgShUk1omCI+euPHftpN8nwuJk4rH+r0H7VcHa0ekhPfgQ0f/hxptA==)**

<Image img={extrude} className="mb-8 bg-contain rounded-md overflow-hidden" />

We do have a bit of a problem though because while want the base to go the full length of the hinge, the pivot should only go half that to make room for the other part of the hinge.
Red scribbles shows what we want to remove.

<Image img={differenceMarkup} className="mb-8 bg-contain rounded-md overflow-hidden" />

The best way around this problem is to extrude the base again separately so to stretch further than the pivot.

```cpp
// ... variables above
linear_extrude(hingeLength/2){
  offset(1)offset(-2)offset(1){
    translate([0,pivotRadius,0]){
      circle(pivotRadius);
    }
    square([pivotRadius,pivotRadius]);
    translate([pivotRadius,0,0]){
      square([baseWidth,baseThickness]);
    }
  }
}
// highlight-start
linear_extrude(hingeLength){
  offset(1)offset(-1)translate([pivotRadius,0,0]){
    square([baseWidth,baseThickness]);
  }
}
// highlight-end
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJyNkE0KwjAQhfc5RRYuEqjYH1yF3sCVCC6kSGynNlhSTVIRpHc31lqmIOomTOabyXsvs1KnSSzIWV0bt5aFam26FOQgLWwqlZ80WJsmr8ZWFa5KI48rpY+wAn309yQUhNRKgzR7uDnTFsAQX8T8TihtytKCYxEfinnMx9aTU+qM1LaWDtguDJCdIMyGCUpzZfIaGKJc9KjrT3tppfH7eBvV2TCMlCY6WOn91Jg7mHxJhnQ70n3J/zl9xH+b+MuCF38AQqCVGw==)**

<Image img={extrude2} className="mb-8 bg-contain rounded-md overflow-hidden" />

## Math Operations

Great, starting to take shape, couple things to go over, first you may have noticed that we just did some inline math with `hingeLength/2`.
This works fine, normal math operations can be preformed and it's fine to mix variables with numbers (also `2` here doesn't count as a magic number since `hingeLength/2` is easy to read as "half of `hingeLength`".

You may notice that we've included the code that forms the hinge base:

```cpp
translate([pivotRadius,0,0]){
  square([baseWidth,baseThickness]);
}
```

within both of the `linear_extrude`s, at first it might seem like we could remove it from first `linear_extrude` since it only goes half the `hingeLength` but this would cause us to loose our internal fillet.

<Image img={noFillet} className="mb-8 bg-contain rounded-md overflow-hidden" />

As it needs to be within `offset` group for this fillet to work.

Okay so we leave it in both `linear_extrude`s but this leaves us in a situation similar to before we introduced variables, in that we have repeated code that would be difficult to determine why to someone reading our code.
Well similar to variables we can solve this with `module`s.
