Tutorials (back to the list of tutorials)

## Various Curve/Surface Modeling Methods

### Offset Curves

You can offset curve with IG.offset(ICurve,double) method and the first argument is a curve to be offset and the second argument is the offset distance. If you put negative value at the offset distance, the offset direction is flipped.

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

IVec[] cpts1=new IVec[]{new IVec(-60,0,0),new IVec(-40,0,0),
new IVec(-40,20,0),new IVec(-60,20,0) };
ICurve curve1 = new ICurve(cpts1).clr(0);
IG.offset(curve1, 5.0).clr(1.0,0,0);

IVec[] cpts2=new IVec[]{new IVec(-20,0,0),new IVec(-10,20,0),
new IVec(-10,0,0) };
ICurve curve2 = new ICurve(cpts2, true).clr(0);
IG.offset(curve2, 5).clr(1.0,0,0);

IVec[] cpts3=new IVec[]{new IVec(10,0,0),new IVec(20,20,0),
new IVec(20,0,0) };
ICurve curve3 = new ICurve(cpts3, 2).clr(0);
IG.offset(curve3, 5).clr(1.,0,0);

IVec[] cpts4=new IVec[]{new IVec(40,0,0),new IVec(50,20,0),
new IVec(50,0,0)};
ICurve curve4 = new ICurve(cpts4, 2, true).clr(0);
IG.offset(curve4, 5).clr(1.,0,0);

IVec[] cpts5=new IVec[]{new IVec(-20,60,0),new IVec(10,60,0),
new IVec(10,60,20)};

ICurve curve5 = new ICurve(cpts5, true).clr(0);
IG.offset(curve5, 5).clr(1.,0,0);

IVec[] cpts6=new IVec[]{new IVec(80,0,0),new IVec(90,0,20),
new IVec(100,0,20),new IVec(100,20,20),
new IVec(80,20,0)};
ICurve curve6 = new ICurve(cpts6, true).clr(0);
IG.offset(curve6, 5).clr(1.,0,0);
```

The offset distance of degree 1 curve can have constant distance between the original curve (polyline) and the offset one, like the curve1 and curve2 in the code above.
However, if the curve's degree is more than 2 and curving, the distance between two curve is not constant, like curve3 and curve4 on the code, because it's simply taking offset vertices of a degree 1 curve as its control points.
If an input curve is not planar curve, offset curve is deformed accordingly and also non-planar, like the curve6 on the code.

### Lofting

You can create a lofted surface by providing array of curves with IG.loft(ICurve[]) for lofting multiple curves, or IG.loft(ICurve,ICurve,) for lofting two curves. Note that all the curves to be lofted need to have the exact same number of control points.

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

IVec[] cpts1 = new IVec[]{new IVec(-50,0,0),new IVec(-30,0,0),
new IVec(-30,-20,0),new IVec(-50,-20,0)};
ICurve curve1 = new ICurve(cpts1).clr(1.,0,1.);

IVec[] cpts2 = new IVec[]{new IVec(-40,20,30),new IVec(-30,20,30),
new IVec(-30,0,30),new IVec(-40,0,30)};
ICurve curve2 = new ICurve(cpts2).clr(1.,0,1.);

// loft of two curves
IG.loft(curve1, curve2).clr(0.2);

IVec[] cpts3 = new IVec[]{new IVec(-20,0,0),new IVec(10,0,0),
new IVec(10,-20,0),new IVec(-20,-20,0)};
ICurve curve3 = new ICurve(cpts3,3,true).clr(1.,1.,0);

IVec[] cpts4 = new IVec[]{new IVec(-10,0,20),new IVec(0,0,20),
new IVec(0,-20,20),new IVec(-10,-20,20)};
ICurve curve4 = new ICurve(cpts4,3,true).clr(1.,1.,0);

IVec[] cpts5 = new IVec[]{new IVec(-20,20,40),new IVec(10,20,40),
new IVec(10,-20,40),new IVec(-20,-20,40)};
ICurve curve5 = new ICurve(cpts5,3,true).clr(1.,1.,0);

// loft of multiple curves in an array (degre=1, straight)
IG.loft(new ICurve[]{ curve3, curve4, curve5 }).clr(0.2);

ICurve curve6 = curve3.dup().mv(40,0,0).clr(0,1.,1);
ICurve curve7 = curve4.dup().mv(40,0,0).clr(0,1.,1);
ICurve curve8 = curve5.dup().mv(40,0,0).clr(0,1.,1);

// degree 2 curved lofting
IG.loft(new ICurve[]{ curve6, curve7, curve8 }, 2 ).clr(0.2);
```

### Extruding Point Array

You can extrude an array of points to create a surface with the method IG.extrude() providing a point array as a profile to be extruded.

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

IVec[] pts1=new IVec[]{new IVec(-50,-20,0),new IVec(-30,-20,0),
new IVec(-30,0,0),new IVec(-50,0,0)};

// extrude points to their normal direction
IG.extrude(pts1, 30).clr(0.2);

IVec[] pts2=new IVec[]{new IVec(-20,0,0),new IVec(10,0,0),
new IVec(10,-20,0),new IVec(-20,-20,0) };

// extrude degree 2 closed profile
IG.extrude(pts2, 2, true, -20).clr(0.2);

IVec[] pts3=new IVec[]{new IVec(20,0,0),new IVec(50,0,0),
new IVec(50,-20,0),new IVec(20,-20,0) };

// extrude in the direction specified by a vector
IG.extrude(pts3, 1, true, new IVec(10,20,30)).clr(0.2);
```

### Extruding Curves

You can extrude a curve in its normal direction or a specified direction to create a surface with IG.extrude() method providing the curve as a profile to be extruded.

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

IVec[] pts1 = new IVec[]{new IVec(-50,-20,0),new IVec(-30,-20,0),
new IVec(-30,0,0),new IVec(-50,0,0)};

ICurve curve1 = new ICurve(pts1).clr(1.,0,1.);

IG.extrude(curve1, 30).clr(0.2);

IVec[] pts2 = new IVec[]{new IVec(-20,0,0),new IVec(10,0,0),
new IVec(10,-20,0),new IVec(-20,-20,0) };
ICurve curve2 = new ICurve(pts2, 2, true).clr(1.,1.,0);

IG.extrude(curve2, -20).clr(0.2);

IVec[] pts3 = new IVec[]{new IVec(20,0,0),new IVec(50,0,0),
new IVec(50,-20,0),new IVec(20,-20,0) };

ICurve curve3 = new ICurve(pts3, 1, true).clr(0,1.,1.);

IG.extrude(curve3, new IVec(10,20,30)).clr(0.2);
```

### Sweeping Point Arrays

You can sweep two set of point array to create a surface with the method IG.sweep(). One set is a profile and another set defines railing line (or curve) to sweep. Note that the size of the profile on the rail whose degree is 2 or more is not accurately same and variable in the exactly same way the offset method creates variable offset distance in degree 2 or more.

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

IVec[] profile1 = new IVec[]{new IVec(5,0,0),new IVec(5,5,0),
new IVec(0,5,0)};
IVec[] rail1 = new IVec[]{new IVec(-40,0,-20),new IVec(-30,0,10),
new IVec(-50,0,20)};
// sweeping open profile and open rail
IG.sweep(profile1, rail1).clr(0.2);

IVec[] profile2 = new IVec[]{new IVec(0,0,0),new IVec(10,0,0),
new IVec(10,10,0),new IVec(0,10,0)};
IVec[] rail2 = new IVec[]{new IVec(0,20,-20),new IVec(0,0,20),
new IVec(0,-20,-20)};
// sweeping degree 2 closed profile and degree 2 open rail
IG.sweep(profile2, 3, true, rail2, 2, false).clr(0.5,0,0);

IVec[] profile3 = new IVec[]{new IVec(-5,0,0),new IVec(0,5,0),
new IVec(0,-5,0)};
IVec[] rail3 = new IVec[]{new IVec(40,20,-20),new IVec(40,0,20),
new IVec(40,-20,-20)};
// sweeping degree 2 closed profile and degree 2 open rail
IG.sweep(profile3, 1, true, rail3, 1, true).clr(0,0,0.5);
```

### Sweeping Curves

You can sweep a profile curve and a railing curve to create a surface by IG.sweep(ICurve,ICurve) method.

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

IVec[] profile1 = new IVec[]{new IVec(0,0,0),new IVec(10,0,0),
new IVec(10,10,0),new IVec(0,10,0)};
ICurve profileCrv1 = new ICurve(profile1,3).clr(0,1.,1.);
IVec[] rail1 = new IVec[]{new IVec(-50,0,-30),new IVec(-30,0,0),
new IVec(-30,0,30)};
ICurve railCrv1 = new ICurve(rail1).clr(1.,1.,0);

// sweeping open profile and open rail
IG.sweep(profileCrv1, railCrv1).clr(0.2);

// ellipse profile
ICurve profileCrv2 = new ICircle(0,0,0,10,5).clr(0,1.,1.);
IVec[] rail2 = new IVec[]{new IVec(0,20,-20),new IVec(0,0,20),
new IVec(0,-20,-20)};
ICurve railCrv2 = new ICurve(rail2, 2).clr(1.,1.,0);

// sweeping degree 2 closed profile and degree 2 open rail
IG.sweep(profileCrv2, railCrv2).clr(0.5,0,0);

IVec[] profile3 = new IVec[]{new IVec(-5,0,0),new IVec(0,5,0),
new IVec(0,-5,0)};
ICurve profileCrv3 = new ICurve(profile3,true).clr(0,1.,1.);
// circle profile
ICurve railCrv3 = new ICircle(new IVec(40,0,0),new IVec(1,0,0),30).clr(1.,1.,0);

// sweeping degree 2 closed profile and degree 2 open rail
IG.sweep(profileCrv3, railCrv3).clr(0,0,0.5);
```

### Circular Pipe Along Curves

You can create circular pipe geometry along a curve with IG.pipe(ICurve,double) method. The first argument is the curve along which the pipe is swept. The second argument is the radius of the pipe. This method has same accuracy issue with sweeping when the degree of the rail curve is 2 or more.

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

ICurve curve1 = new ICurve(new IVec(-40,0,-20),new IVec(-30,20,30));
IG.pipe(curve1, 2).clr(.2);

ICurve curve2 = new ICurve(new IVec[]{new IVec(-10,0,-30),
new IVec(-10,-10,-10),
new IVec(-10,10,10),
new IVec(-10,0,30) });
IG.pipe(curve2,6).clr(0.5,0,0);

ICurve curve3 = new ICircle(new IVec(20,0,0),new IVec(1,0,0),20);
IG.pipe(curve3,4).clr(1.0,0,0.5);

ICurve curve4 = new ICurve(new IVec[]{new IVec(50, -20, 20),
new IVec(40, 30, 0),
new IVec(70, 0, 40),
new IVec(50, -10, -10),
new IVec(60, -40, -10)},
2, true);
IG.pipe(curve4, 4).clr(0,0,0.8);
```

### Rectangular Pipe Along Curves

In the same way with creating circular pipes, you can also create rectangular or square pipes with IG.rectPipe(ICurve,double,double) or IG.squarePipe(ICurve,double).

```import processing.opengl.*;
import igeo.*;

size(480, 360, IG.GL);

ICurve curve1 = new ICurve(new IVec(-40,0,-20),new IVec(-30,20,30));
IG.rectPipe(curve1, 10,2).clr(.2);

ICurve curve2 = new ICurve(new IVec[]{new IVec(-10,0,-30),
new IVec(-10,-10,-10),
new IVec(-10,10,10),
new IVec(-10,0,30) });
IG.rectPipe(curve2,5,10).clr(0.5,0,0);

ICurve curve3 = new ICircle(new IVec(20,0,0),new IVec(1,0,0),20);
IG.squarePipe(curve3,8).clr(1.0,0,0.5);

ICurve curve4 = new ICurve(new IVec[]{new IVec(50, -20, 20),
new IVec(40, 30, 0),
new IVec(70, 0, 40),
new IVec(50, -10, -10),
new IVec(60, -40, -10)},
2, true);
IG.squarePipe(curve4,8).clr(0,0,0.8);
```

(back to the list of tutorials)