> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/math/geometry/line.md).

# Line

### Introduction

A line is defined by two endpoints, so in mathematical terms it is actually a segment. Lines can be used to perform 2D collisions against any other shapes.

### Line Sample

The following sample creates a line, a [Circle](/flatredball/api/flatredball/math/geometry/circle.md), and an [AxisAlignedRectangle](/flatredball/api/flatredball/math/geometry/shapecollection/axisalignedrectangles.md). The line is controlled with the keyboard and it changes colors when it collides with the other two shapes. Add the following using statements:

```csharp
using Microsoft.Xna.Framework.Graphics;
using FlatRedBall.Math.Geometry;
```

Add the following at class scope:

```csharp
AxisAlignedRectangle rectangle;
Circle circle;
Line line;
```

Add the following in Initialize after Initializing FlatRedBall:

```csharp
rectangle = ShapeManager.AddAxisAlignedRectangle();
rectangle.X = 50;
rectangle.Color = Color.Red;

circle = ShapeManager.AddCircle();
circle.X = -50;
circle.Color = Color.Blue;

// Creates a horizontal line of length 2 by default.
line = ShapeManager.AddLine();
```

Add the following in Update:

```csharp
InputManager.Keyboard.ControlPositionedObject(line);

if (line.CollideAgainst(rectangle))
{
    line.Color = rectangle.Color;
}
else if (line.CollideAgainst(circle))
{
    line.Color = circle.Color;
}
else
{
    line.Color = Color.White;
}
```

![LineTutorial.png](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-cd5b5cb2fd807c7f572fc268017b72b6b7896cbc%2Fmigrated_media-LineTutorial.png?alt=media) Note that FlatRedBall expects shape colors to be pre-multiplied. Therefore a half-transparent red value would have a R,G,B,A value of (128,0,0,128).

### RelativePoint Properties

A line can be modified by changing both its [PositionedObject](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) properties as well as through the RelativePoint property. The following code connects two rectangles with a line. Add the following in Update:

```csharp
using Microsoft.Xna.Framework.Graphics;
using FlatRedBall.Math.Geometry;
```

Add the following in Initialize after Initializing FlatRedBall:

```csharp
AxisAlignedRectangle rectangle1 = ShapeManager.AddAxisAlignedRectangle();
rectangle1.Position = new Vector3(-2, 4, 0);
rectangle1.Color = Color.Green;

AxisAlignedRectangle rectangle2 = ShapeManager.AddAxisAlignedRectangle();
rectangle2.Position = new Vector3(4, -1, 0);
rectangle2.Color = Color.Red;

// The most common way to make a connecting line is to set the line's
// positions at one of the end points, then simply calculate the relative
// position of the other endpoint.  That is, the position of the line does
// not have to be the midpoint.
Line connectingLine = ShapeManager.AddLine();
connectingLine.Position = rectangle1.Position;
connectingLine.RelativePoint1 = new Point3D(0, 0, 0);

// Now just get the position of rectangle2 relative to rectangle1
// and put that in RelativePoint2.
connectingLine.RelativePoint2 = new Point3D(
    rectangle2.X - rectangle1.X,
    rectangle2.Y - rectangle1.Y,
    rectangle2.Z - rectangle1.Z); // will be 0 in our case
```

![ConnectedRectangles.png](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-9c84409ea52147caf5a15963dfd8ab240ea96d1b%2Fmigrated_media-ConnectedRectangles.png?alt=media)

### Line limitations

* Lines, just like any other [Shapes](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php), can either be drawn on top of or below types that can sort with each other, such as [Sprites](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) and [Texts](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php). They will not sort according to their Z value.
* Lines must be one pixel thick. Thicker lines are not supported.
* Lines can only draw solid colors - patterns and gradients are not supported.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/math/geometry/line.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
