---
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]);
    }
  }
}
```

<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
```

<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.
