# HowTo:Use constructive solid geometry

## Constructive Solid Geometry

Constructive Solid Geometry (CSG) is a powerful tool to combine primitive objects to create more complex objects.

## Union

The union object is a way to apply a universal group to a family of objects. Their appearance does not change at all while using this. To declare a union object:

```#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
union{
box{ -1, 1 pigment{ Blue } }
sphere{ 0, 1.375 pigment{ Red } }
}
```

Since union creates a single object from multiple ones, we can apply textures and other transformations to all objects.

```#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
union{
box{ -1, 1 }
sphere{ 0, 1.375 }
pigment{ Red }
}
```

The above code will give the resulting object a pigment of Red, without us having to change the Blue box to Red and then others if we decide to change the color of the whole object. The resultant image, rendered with anti-aliasing, is shown below:

Union Example

## Intersections

Like union, intersect creates a single object out of many, however it applies a calculation that will only result in what parts of the listed objects that intersect with each other. Intersect inherits from union so anything you can do to a union object you can do in a intersect object.

So if we take our previous code and change it around a small bit:

```#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
intersection{
box{ -1, 1 }
sphere{ 0, 1.375 }
pigment{ Red }
}
```

What you'll see is a sphere with it's sides cut off because they are outside the intersection of the box and sphere. The corners are rounded because the corners of the box are outside the sphere.

Intersection Example

## Difference

Again, as with intersection and union, difference creates a single object out of many. The difference object is created by taking the initial object in the list and cutting away the other listed objects from it. A thing to note is that any of the CSG operations can be embedded with each other:

```#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
difference{
intersection{
box{ -1, 1 }
sphere{ 0, 1.375 }
pigment{ Red }
}
union{
cylinder{ -2*y, 2*y, 0.5 }
cylinder{ -2*x, 2*x, 0.5 }
cylinder{ -2*z, 2*z, 0.5 }
pigment{ Blue }
}
}
```
Difference Example

## Merge

Merge is much like union, with the main difference of it actually treats all objects as one instead of just a list of objects in one family. This is more visible when using semi-clear or fully clear objects, such as water or glass-like objects.

For example, take the following code:

```#include "colors.inc"
#include "textures.inc"
camera{
location <0, 2, -10>
look_at 1*y
}
light_source{ 1000 White }
plane{ y, -1 pigment{ White } }
union{
sphere{ 0, 1 translate 0.5*x }
sphere{ 0, 1 translate -0.5*x }
texture{ Glass2 }
}
merge{
sphere{ 0, 1 translate 0.5*x }
sphere{ 0, 1 translate -0.5*x }
texture{ Glass2 }
translate 2*y
}
```

On the bottom pair of spheres we can still see the edges of the spheres on the inside. On the top pair we don't. That's the difference between union and merge.

Merge Example