Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ install(DIRECTORY "include" DESTINATION ".")

add_library(${PROJECT_NAME} SHARED
"src/angle/angle.cpp"
"src/geometry/line.cpp"
"src/geometry/point_2.cpp"
"src/geometry/point_3.cpp"
"src/matrix/matrix.cpp"
Expand Down
51 changes: 51 additions & 0 deletions include/keisan/geometry/line.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2026 ICHIRO ITS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef KEISAN__GEOMETRY__LINE_HPP_
#define KEISAN__GEOMETRY__LINE_HPP_

#include "keisan/geometry/point_2.hpp"

namespace keisan
{

struct Line
{
Line();
Line(double a, double b, double c);

static Line from_points(const Point2 & p1, const Point2 & p2);
static Line from_point(const Point2 & point, const Angle<double> & angle);

double distance(const Point2 & point) const;

std::optional<double> slope() const;
Angle<double> angle() const;

std::optional<Point2> intersection(const Line & other) const;

double a;
double b;
double c;
};

} // namespace keisan

#endif // KEISAN__GEOMETRY__LINE_HPP_
78 changes: 78 additions & 0 deletions src/geometry/line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2026 ICHIRO ITS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "keisan/geometry/line.hpp"

namespace keisan
{

Line::Line() {}

Line::Line(double a, double b, double c) : a(a), b(b), c(a) {}

Line Line::from_points(const Point2 & p1, const Point2 & p2)
{
double a = p2.y - p1.y;
double b = p1.x - p2.x;
double c = -(a * p1.x + b * p1.y);
return Line(a, b, c);
}

Line Line::from_point(const Point2 & point, const Angle<double> & angle)
{
double a = -angle.sin();
double b = angle.cos();
double c = -(a * point.x + b * point.y);

return Line(a, b, c);
}

double Line::distance(const Point2 & point) const
{
double denominator = std::hypot(a, b);

if (denominator < 1e-9) {
throw std::runtime_error(
"Invalid line equations: coefficients 'a' and 'b' cannot both be zero.");
}

return std::abs(a * point.x + b * point.y + c) / denominator;
}

std::optional<Point2> Line::intersection(const Line & other) const
{
double determinant = a * other.b - other.a * b;
if (std::abs(determinant) < 1e-9) {
return std::nullopt; // Lines are parallel or coincident
}
double x = (b * other.c - other.b * c) / determinant;
double y = (other.a * c - a * other.c) / determinant;
return Point2{x, y};
}

std::optional<double> Line::slope() const
{
if (std::abs(b) < 1e-9) return std::nullopt; // Line is vertical
return -a / b;
}

Angle<double> Line::angle() const { return signed_arctan(a, -b); }

} // namespace keisan
Loading