#pragma region CPL License
/*
Nuclex Native Framework
Copyright (C) 2002-2015 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#pragma endregion // CPL License
#ifndef NUCLEX_GEOMETRY_LINES_TRACERS_SEGMENT2TRACER_H
#define NUCLEX_GEOMETRY_LINES_TRACERS_SEGMENT2TRACER_H
#include "Nuclex/Geometry/Config.h"
#include "Nuclex/Geometry/Lines/Line2.h"
#include "Nuclex/Geometry/Lines/Segment2.h"
#include "Nuclex/Geometry/LineContacts.h"
namespace Nuclex { namespace Geometry { namespace Lines { namespace Tracers {
// ------------------------------------------------------------------------------------------- //
/// Determines contacts between 2D lines and line segments
template
class Segment2Tracer {
/// Determines where a line crosses a line segment
/// Line whose time of crossing will be determined
/// Line segment that might be crossed
/// The time at which the line crosses the line segment
public: static LineContacts FindLineContacts(
const Lines::Line2 &line, const Lines::Segment2 &segment
) {
Vector2 segmentDirection = (segment.End - segment.Start);
// Time of intersection of the segment with the traced line. This allows us to see
// if the segment hits the traced line inside its length.
TScalar s = (
line.Direction.X * (line.Origin.Y - segment.Start.Y) -
line.Direction.Y * (line.Origin.X - segment.Start.X)
) / (line.Direction.X * segmentDirection.Y - segmentDirection.X * line.Direction.Y);
if((s >= 0) && (s < 1)) {
TScalar t = (
segmentDirection.X * (line.Origin.Y - segment.Start.Y) -
segmentDirection.Y * (line.Origin.X - segment.Start.X)
) / (line.Direction.X * segmentDirection.Y - segmentDirection.X * line.Direction.Y);
return LineContacts(t, t);
} else {
return LineContacts::None;
}
}
};
// ------------------------------------------------------------------------------------------- //
}}}} // namespace Nuclex::Geometry::Lines::Tracers
#endif // NUCLEX_GEOMETRY_LINES_TRACERS_SEGMENT2TRACER_H