## com.bbn.openmap.proj Class DrawUtil

```java.lang.Object com.bbn.openmap.proj.DrawUtil
```

`public class DrawUtilextends java.lang.Object`

Drawing utility functions.

Method Summary
`static java.awt.Point[]` ```bresenham_line(int x1, int y1, int x2, int y2)```
Bresenham's line algorithm.
`static java.awt.Point[]` ```bresenham_line(java.awt.Point pt1, java.awt.Point pt2)```
Bresenham's line algorithm.
`static float` ```closestPolyDistance(float[] xpts, float[] ypts, double ptx, double pty, boolean connected)```
Returns the distance from Point (x,y) to the closest line segment in the Poly (int[] xpts, int[] ypts).
`static float` ```distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)```
Distance to closest endpoint.
`static double` ```distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3)```
Computes the distance from a point to a line segment.
`static double` ```distance(double x1, double y1, double x2, double y2)```
2D distance formula.
`static float` ```distance(float x1, float y1, float x2, float y2)```
2D distance formula.
`static float` ```distance(int x1, int y1, int x2, int y2)```
2D distance formula.
`static java.util.List<int[]>` ```generateWideLine(int lw, int x1, int y1, int x2, int y2)```
Generates a line with width lw, returns an ArrayList of 4 x-y coords.
`static java.util.List<int[]>` ```generateWidePoly(int lw, int[] xpts, int[] ypts, int[] altx, boolean connect)```
Generates a polygon or polyline with positive width lw.
`static java.util.List<int[]>` ```generateWidePoly(int lw, int len, int[] xpts, int[] ypts, int[] altx, boolean connect)```
Generates a polygon or polyline with positive width lw.
`static boolean` ```inside_polygon(float[] xpts, float[] ypts, double ptx, double pty)```
Tests if a point is inside a polygon.
`static int[]` ```lineSegments(int x1, int y1, int x2, int y2, int n, boolean include_last, int[] ret_val)```
Generate additional vertices between two points.
`static java.awt.Point[]` ```lineSegments(java.awt.Point pt1, java.awt.Point pt2, int n, boolean include_last)```
Returns n or n+1 points along a line.
`static float` ```OLD_distance_to_line(int x1, int y1, int x2, int y2, int x, int y)```
Deprecated. USE THE NEW FUNCTION
`static float` ```perpendicular_distance_to_line(int x1, int y1, int x2, int y2, int x3, int y3)```
Compute perpendicular distance from point to line.
`static int` ```pixel_distance(int x1, int y1, int x2, int y2)```
Calculate the "pixel distance" between two points (squaring not involved).

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### lineSegments

```public static final int[] lineSegments(int x1,
int y1,
int x2,
int y2,
int n,
boolean include_last,
int[] ret_val)```
Generate additional vertices between two points.

Parameters:
`x1` - x coord
`y1` - y coord
`x2` - x coord
`y2` - y coord
`n` - num segments
`include_last` - include the last one?
`ret_val` - the array to put them in
Returns:
int[] ret_val

### lineSegments

```public static final java.awt.Point[] lineSegments(java.awt.Point pt1,
java.awt.Point pt2,
int n,
boolean include_last)```
Returns n or n+1 points along a line.

Parameters:
`pt1` - point
`pt2` - point
`n` - count
`include_last` - boolean
Returns:
Point[]

### bresenham_line

```public static final java.awt.Point[] bresenham_line(java.awt.Point pt1,
java.awt.Point pt2)```
Bresenham's line algorithm.

Returns an array of points to draw.

Parameters:
`pt1` - point
`pt2` - point
Returns:
Point[]

### bresenham_line

```public static final java.awt.Point[] bresenham_line(int x1,
int y1,
int x2,
int y2)```
Bresenham's line algorithm.

Parameters:
`x1` - horizontal pixel window location of first point.
`y1` - vertical pixel window location of first point.
`x2` - horizontal pixel window location of second point.
`y2` - vertical pixel window location of second point.
Returns:
Point[]

### inside_polygon

```public static final boolean inside_polygon(float[] xpts,
float[] ypts,
double ptx,
double pty)```
Tests if a point is inside a polygon.

Parameters:
`xpts` - horizontal pixel window points of polygon.
`ypts` - vertical pixel window points of polygon.
`ptx` - horizontal pixel window points of location
`pty` - vertical pixel window points of location.
Returns:
boolean

### closestPolyDistance

```public static final float closestPolyDistance(float[] xpts,
float[] ypts,
double ptx,
double pty,
boolean connected)```
Returns the distance from Point (x,y) to the closest line segment in the Poly (int[] xpts, int[] ypts).

This procedure assumes that xpts.length == ypts.length.

Parameters:
`xpts` - X points of the polygon
`ypts` - Y points of the polygon
`ptx` - x location of the point
`pty` - y location of the point
`connected` - polyline or polygon

### distance

```public static final float distance(float x1,
float y1,
float x2,
float y2)```
2D distance formula.

Parameters:
`x1` - x coord
`y1` - y coord
`x2` - x coord
`y2` - y coord
Returns:
float distance

### distance

```public static final float distance(int x1,
int y1,
int x2,
int y2)```
2D distance formula.

Parameters:
`x1` - x coord
`y1` - y coord
`x2` - x coord
`y2` - y coord
Returns:
float distance

### distance

```public static final double distance(double x1,
double y1,
double x2,
double y2)```
2D distance formula.

Parameters:
`x1` - x coord
`y1` - y coord
`x2` - x coord
`y2` - y coord
Returns:
double distance

### pixel_distance

```public static final int pixel_distance(int x1,
int y1,
int x2,
int y2)```
Calculate the "pixel distance" between two points (squaring not involved).

Parameters:
`x1` - x coord
`y1` - y coord
`x2` - x coord
`y2` - y coord
Returns:
int pixel distance

### distance_to_endpoint

```public static final float distance_to_endpoint(int x1,
int y1,
int x2,
int y2,
int x,
int y)```
Distance to closest endpoint.

Parameters:
`x1` - x coord
`y1` - y coord
`x2` - x coord
`y2` - y coord
`x` - x coord of point
`y` - y coord of point
Returns:
float distance to endpoint

### OLD_distance_to_line

```public static final float OLD_distance_to_line(int x1,
int y1,
int x2,
int y2,
int x,
int y)```
Deprecated. USE THE NEW FUNCTION

Compute distance from point to line segment.

Compute the distance from point (x,y) to a line by computing the perpendicular line from (x,y) to the line and finding the intersection of this perpendicular and the line. If the intersection is on the line segment, then the distance is the distance from the mouse to the intersection, otherwise it is the distance from (x,y) to the nearest endpoint.

Equations used to compute distance:

• m = (y2-y1)/(x2-x1) slope of the line
• y = mx + b equation of the line
• c = -1/m slope of line perpendicular to it
• y = cx + d equation of perpendicular line
• xi = (d-b)/(m-c) x-intersection, from equating the 2 line equations
• y1 = c* xi + d y-intersection
• distance = sqrt(sqr(x-xi) + sqr(y-yi)) distance between two points

Parameters:
`x1` - line x coord1
`y1` - line y coord1
`x2` - line x coord2
`y2` - line y coord2
`x` - point x coord
`y` - point y coord
Returns:
float distance to line segment

### perpendicular_distance_to_line

```public static final float perpendicular_distance_to_line(int x1,
int y1,
int x2,
int y2,
int x3,
int y3)```
Compute perpendicular distance from point to line.

Parameters:
`x1` - line x coord1
`y1` - line y coord1
`x2` - line x coord2
`y2` - line y coord2
`x3` - point x coord
`y3` - point y coord
Returns:
float distance to line

### distance_to_line

```public static final double distance_to_line(double x1,
double y1,
double x2,
double y2,
double x3,
double y3)```
Computes the distance from a point to a line segment.

Variable usage as follows:

• x12 x distance from the first endpoint to the second.
• y12 y distance from the first endpoint to the second.
• x13 x distance from the first endpoint to point being tested.
• y13 y distance from the first endpoint to point being tested.
• x23 x distance from the second endpoint to point being tested.
• y23 y distance from the second endpoint to point being tested.
• D12 Length of the line segment.
• pp distance along the line segment to the intersection of the perpendicular from the point to line extended.
Procedure:

Compute D12, the length of the line segment. Compute pp, the distance to the perpendicular. If pp is negative, the intersection is before the start of the line segment, so return the distance from the start point. If pp exceeds the length of the line segment, then the intersection is beyond the end point so return the distance of the point from the end point. Otherwise, return the absolute value of the length of the perpendicular line. The sign of the length of the perpendicular line indicates whether the point lies to the right or left of the line as one travels from the start point to the end point.

Parameters:
`x1` - line x coord1
`y1` - line y coord1
`x2` - line x coord2
`y2` - line y coord2
`x3` - point x coord
`y3` - point y coord
Returns:
float distance to line segment

### generateWideLine

```public static java.util.List<int[]> generateWideLine(int lw,
int x1,
int y1,
int x2,
int y2)```
Generates a line with width lw, returns an ArrayList of 4 x-y coords.

Parameters:
`lw` - line width
`x1` - line x coord1
`y1` - line y coord1
`x2` - line x coord2
`y2` - line y coord2
Returns:
List int[] of x[], y[]

### generateWidePoly

```public static final java.util.List<int[]> generateWidePoly(int lw,
int[] xpts,
int[] ypts,
int[] altx,
boolean connect)```
Generates a polygon or polyline with positive width lw.

Returns ArrayList of x-y array pairs of coordinates of polygon segments. the parameter altx must either be null, or an alternate array of points to draw.

Parameters:
`lw` - line width
`xpts` - int[] x coords
`ypts` - int[] y coords
`altx` - int[] altx coords
`connect` - polygon or polyline?
Returns:
List int[] of x[], y[]

### generateWidePoly

```public static final java.util.List<int[]> generateWidePoly(int lw,
int len,
int[] xpts,
int[] ypts,
int[] altx,
boolean connect)```
Generates a polygon or polyline with positive width lw.

Returns ArrayList of x-y array pairs of coordinates of polygon segments. the parameter altx must either be null, or an alternate array of points to draw.

Parameters:
`lw` - line width
`len` - numcoords
`xpts` - int[] x coords
`ypts` - int[] y coords
`altx` - int[] altx coords
`connect` - polygon or polyline?
Returns:
List int[] of x[], y[]

Copyright (C) BBNT Solutions LLC; See http://openmap.bbn.com/ for details