| 2971 | } |
| 2972 | |
| 2973 | bool PixelGameEngine::ClipLineToDrawTarget(olc::vi2d& in_p1, olc::vi2d& in_p2) |
| 2974 | { |
| 2975 | olc::vi2d vDrawTargetSize{ (int32_t)GetDrawTargetWidth(), (int32_t)GetDrawTargetHeight() }; |
| 2976 | |
| 2977 | // https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm |
| 2978 | static constexpr int SEG_I = 0b0000, SEG_L = 0b0001, SEG_R = 0b0010, SEG_B = 0b0100, SEG_T = 0b1000; |
| 2979 | auto Segment = [&vDrawTargetSize = vDrawTargetSize](const olc::vi2d& v) |
| 2980 | { |
| 2981 | int i = SEG_I; |
| 2982 | if (v.x < 0) i |= SEG_L; else if (v.x > vDrawTargetSize.x) i |= SEG_R; |
| 2983 | if (v.y < 0) i |= SEG_B; else if (v.y > vDrawTargetSize.y) i |= SEG_T; |
| 2984 | return i; |
| 2985 | }; |
| 2986 | |
| 2987 | int s1 = Segment(in_p1), s2 = Segment(in_p2); |
| 2988 | |
| 2989 | while (true) |
| 2990 | { |
| 2991 | if (!(s1 | s2)) return true; |
| 2992 | else if (s1 & s2) return false; |
| 2993 | else |
| 2994 | { |
| 2995 | int s3 = s2 > s1 ? s2 : s1; |
| 2996 | olc::vi2d n; |
| 2997 | if (s3 & SEG_T) { n.x = in_p1.x + (in_p2.x - in_p1.x) * (vDrawTargetSize.y - in_p1.y) / (in_p2.y - in_p1.y); n.y = vDrawTargetSize.y; } |
| 2998 | else if (s3 & SEG_B) { n.x = in_p1.x + (in_p2.x - in_p1.x) * (0 - in_p1.y) / (in_p2.y - in_p1.y); n.y = 0; } |
| 2999 | else if (s3 & SEG_R) { n.x = vDrawTargetSize.x; n.y = in_p1.y + (in_p2.y - in_p1.y) * (vDrawTargetSize.x - in_p1.x) / (in_p2.x - in_p1.x); } |
| 3000 | else if (s3 & SEG_L) { n.x = 0; n.y = in_p1.y + (in_p2.y - in_p1.y) * (0 - in_p1.x) / (in_p2.x - in_p1.x); } |
| 3001 | if (s3 == s1) { in_p1 = n; s1 = Segment(in_p1); } |
| 3002 | else { in_p2 = n; s2 = Segment(in_p2); } |
| 3003 | } |
| 3004 | } |
| 3005 | return true; |
| 3006 | } |
| 3007 | |
| 3008 | void PixelGameEngine::EnablePixelTransfer(const bool bEnable) |
| 3009 | { |
nothing calls this directly
no outgoing calls
no test coverage detected