MCPcopy Create free account
hub / github.com/OneLoneCoder/olcPixelGameEngine / line

Class line

utilities/olcUTIL_Geometry2D.h:559–633  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

557 // Defines a line segment
558 template<typename T>
559 struct line
560 {
561 olc::v_2d<T> start;
562 olc::v_2d<T> end;
563
564 inline line(const olc::v_2d<T>& s = { T(0), T(0) },
565 const olc::v_2d<T>& e = { T(0), T(0) })
566 : start(s), end(e)
567 { }
568
569 // Get vector pointing from start to end
570 inline constexpr olc::v_2d<T> vector() const
571 {
572 return (end - start);
573 }
574
575 // Get length of line
576 inline constexpr T length()
577 {
578 return vector().mag();
579 }
580
581 // Get length of line^2
582 inline constexpr T length2()
583 {
584 return vector().mag2();
585 }
586
587
588
589 // Given a real distance, get point along line
590 inline constexpr olc::v_2d<T> rpoint(const T& distance) const
591 {
592 return start + vector().norm() * distance;
593 }
594
595 // Given a unit distance, get point along line
596 inline constexpr olc::v_2d<T> upoint(const T& distance) const
597 {
598 return start + vector() * distance;
599 }
600
601 // Return which side of the line does a point lie
602 inline constexpr int32_t side(const olc::v_2d<T>& point) const
603 {
604 double d = vector().cross(point - start);
605 if (d < 0)
606 return -1;
607 else
608 if (d > 0)
609 return 1;
610 else
611 return 0;
612 }
613
614 // Returns line equation "mx + a" coefficients where:
615 // x: m
616 // y: a

Callers 2

sideMethod · 0.85
perimeterMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected