java.lang.Object
net.regsirius06.engine.core.graphics.rays.Line2D

public class Line2D extends Object
Represents a 2D line in the Cartesian plane. A Line2D object can be constructed using the slope and y-intercept, two points, or an angle and a point.

The class provides various methods to work with a line, including the ability to:

  • Calculate the y-coordinate for a given x-coordinate on the line
  • Calculate the x-coordinate for a given y-coordinate on the line
  • Compute the angle of the line with respect to the x-axis
  • Rotate the line around a specified point
  • Get the perpendicular (normal) line to the current line

This class is useful for geometric and raycasting algorithms that involve line manipulation and is particularly relevant in the rendering of labyrinths and other 2D environments.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Line2D(double k, double b)
    Constructs a Line2D object using the slope (k) and the y-intercept (b).
    Line2D(double x0, double y0, double angle)
    Constructs a Line2D object that passes through a specified point (x0, y0) with a given angle.
    Line2D(double x0, double y0, double x1, double y1)
    Constructs a Line2D object passing through two points (x0, y0) and (x1, y1).
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Calculates the angle of the line with respect to the x-axis.
    double
    getX(double y)
    Calculates the x-coordinate for a given y-coordinate based on the line equation (x = (y - b) / k).
    double
    getY(double x)
    Calculates the y-coordinate for a given x-coordinate based on the line equation (y = kx + b).
    Calculates and returns a new Line2D object that is perpendicular (normal) to this line.
    rotate(double x0, double angle)
    Rotates the line around a given point (x0, y0) by a specified angle in radians.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Line2D

      public Line2D(double k, double b)
      Constructs a Line2D object using the slope (k) and the y-intercept (b).
      Parameters:
      k - The slope of the line. It represents the change in y for a given change in x.
      b - The y-intercept of the line. It is the point where the line crosses the y-axis.
    • Line2D

      public Line2D(double x0, double y0, double angle)
      Constructs a Line2D object that passes through a specified point (x0, y0) with a given angle. The angle is specified in radians, where 0 radians represents a horizontal line.
      Parameters:
      x0 - The x-coordinate of a point through which the line passes.
      y0 - The y-coordinate of the point.
      angle - The angle of the line in radians with respect to the x-axis.
    • Line2D

      public Line2D(double x0, double y0, double x1, double y1)
      Constructs a Line2D object passing through two points (x0, y0) and (x1, y1). The slope is calculated as the difference in y-coordinates divided by the difference in x-coordinates.
      Parameters:
      x0 - The x-coordinate of the first point.
      y0 - The y-coordinate of the first point.
      x1 - The x-coordinate of the second point.
      y1 - The y-coordinate of the second point.
  • Method Details

    • getY

      public double getY(double x)
      Calculates the y-coordinate for a given x-coordinate based on the line equation (y = kx + b).
      Parameters:
      x - The x-coordinate for which to calculate the corresponding y-coordinate.
      Returns:
      The y-coordinate corresponding to the given x-coordinate on this line.
    • getX

      public double getX(double y)
      Calculates the x-coordinate for a given y-coordinate based on the line equation (x = (y - b) / k).
      Parameters:
      y - The y-coordinate for which to calculate the corresponding x-coordinate.
      Returns:
      The x-coordinate corresponding to the given y-coordinate on this line.
    • getAngle

      public double getAngle()
      Calculates the angle of the line with respect to the x-axis. The angle is calculated using the arctangent of the slope (atan(k)).
      Returns:
      The angle of the line in radians with respect to the positive x-axis.
    • rotate

      public Line2D rotate(double x0, double angle)
      Rotates the line around a given point (x0, y0) by a specified angle in radians. The line will be rotated while keeping the point (x0, y0) fixed.
      Parameters:
      x0 - The x-coordinate of the rotation center.
      angle - The angle of rotation in radians.
      Returns:
      A new Line2D object representing the rotated line.
    • normal

      public Line2D normal()
      Calculates and returns a new Line2D object that is perpendicular (normal) to this line. The normal line has a slope of -1/k and passes through the same y-intercept.
      Returns:
      A new Line2D object representing the normal line to this line.