├── .gitignore ├── LICENSE ├── README.md ├── geometry.py ├── geometry_tests.py └── raycasting.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # raycasting -------------------------------------------------------------------------------- /geometry.py: -------------------------------------------------------------------------------- 1 | import functools 2 | import dataclasses 3 | import typing 4 | import math 5 | 6 | DISTANT_POINT = 100 7 | 8 | 9 | # Floating point math is hard, and trying to find a point on a line 10 | # can result in some mismatches in floating point values, so we go for "close" 11 | def in_range(min_, max_, value): 12 | return (min_ - 0.0000001) <= value <= (max_ + 0.0000001) 13 | 14 | 15 | class Point(typing.NamedTuple): 16 | x: float 17 | y: float 18 | 19 | def __add__(self, other): 20 | return Point(self.x + other.x, self.y + other.y) 21 | 22 | def __sub__(self, other): 23 | return Point(self.x - other.x, self.y - other.y) 24 | 25 | 26 | @dataclasses.dataclass(unsafe_hash=True) 27 | class Segment: 28 | start: Point 29 | end: Point 30 | 31 | def parallel(self, other): 32 | # Todo - de-duplicate this code 33 | x1, y1 = self.start.x, self.start.y 34 | x2, y2 = self.end.x, self.end.y 35 | 36 | x3, y3 = other.start.x, other.start.y 37 | x4, y4 = other.end.x, other.end.y 38 | 39 | # Calculate the denominator of the t and u values in the parametric equations of the two segments 40 | return ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)) == 0 41 | 42 | def intersection(self, other): 43 | if not ( 44 | self.min_x <= other.max_x 45 | and self.max_x >= other.min_x 46 | and self.min_y <= other.max_y 47 | and self.max_y >= other.min_y 48 | ): 49 | return None 50 | 51 | # This version is cribbed from ChatGPT, and it passes our tests for 52 | # line intersection calculations 53 | 54 | # Calculate the differences between the start and end points of the two segments 55 | 56 | x1, y1 = self.start.x, self.start.y 57 | x2, y2 = self.end.x, self.end.y 58 | 59 | x3, y3 = other.start.x, other.start.y 60 | x4, y4 = other.end.x, other.end.y 61 | 62 | # Calculate the denominator of the t and u values in the parametric equations of the two segments 63 | denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1) 64 | 65 | # Check if the two segments are parallel (i.e., their parametric equations don't intersect) 66 | if denominator == 0: 67 | return None 68 | 69 | # Calculate the t and u values in the parametric equations of the two segments 70 | t = ((x3 - x1) * (y4 - y3) - (y3 - y1) * (x4 - x3)) / denominator 71 | u = ((x1 - x2) * (y3 - y1) - (y1 - y2) * (x3 - x1)) / denominator 72 | 73 | # Check if the two segments intersect 74 | if 0 <= t <= 1 and 0 <= u <= 1: 75 | # Calculate the point of intersection 76 | x = x1 + t * (x2 - x1) 77 | y = y1 + t * (y2 - y1) 78 | return Point(x, y) 79 | else: 80 | return None 81 | 82 | @functools.cached_property 83 | def min_x(self): 84 | return min(self.start.x, self.end.x) 85 | 86 | @functools.cached_property 87 | def max_x(self): 88 | return max(self.start.x, self.end.x) 89 | 90 | @functools.cached_property 91 | def min_y(self): 92 | return min(self.start.y, self.end.y) 93 | 94 | @functools.cached_property 95 | def max_y(self): 96 | return max(self.start.y, self.end.y) 97 | 98 | def in_bounds(self, p: Point): 99 | return in_range(self.min_x, self.max_x, p.x) and in_range( 100 | self.min_y, self.max_y, p.y 101 | ) 102 | 103 | def to_ray(self): 104 | if self.start == self.end: 105 | # no possible valid Ray object 106 | raise RuntimeError("Cannot create Ray from identical segment points") 107 | 108 | # Correct from angle above x axis as returned by atan2, to angle 109 | # away from y axis, as is in our coordinate system 110 | return Ray( 111 | self.start, 112 | -math.atan2(self.end.y - self.start.y, self.end.x - self.start.x) 113 | + math.pi / 2, 114 | ) 115 | 116 | 117 | @dataclasses.dataclass 118 | class Ray: 119 | start: Point 120 | angle: float # Angle from the y-axis, right. "compass coordinates" 121 | 122 | def end_point(self, distance): 123 | return Point( 124 | self.start.x + (math.sin(self.angle) * distance), 125 | self.start.y + (math.cos(self.angle) * distance), 126 | ) 127 | 128 | def to_segment(self, distance=DISTANT_POINT): 129 | return Segment(self.start, self.end_point(distance)) 130 | 131 | 132 | def intersect_ray(ray: Ray, segments): 133 | return intersecting_segments(ray.to_segment(), segments) 134 | 135 | 136 | def intersecting_segments(input_: Segment, segments): 137 | result = [] 138 | 139 | for segment in segments: 140 | intersection = input_.intersection(segment) 141 | 142 | if intersection is not None: 143 | result.append( 144 | (math.dist(input_.start, intersection), intersection, segment) 145 | ) 146 | 147 | return result 148 | -------------------------------------------------------------------------------- /geometry_tests.py: -------------------------------------------------------------------------------- 1 | import geometry 2 | import math 3 | import pytest 4 | 5 | import raycasting 6 | 7 | 8 | def test_adding_points(): 9 | p1 = geometry.Point(1, 2) 10 | p2 = geometry.Point(3, 4) 11 | result = p1 + p2 12 | assert result.x == 4 13 | assert result.y == 6 14 | 15 | 16 | def test_segment_properties(): 17 | def expected_values(segment, min_x, min_y, max_x, max_y, ray): 18 | assert segment.min_x == min_x 19 | assert segment.min_y == min_y 20 | assert segment.max_x == max_x 21 | assert segment.max_y == max_y 22 | 23 | assert segment.to_ray() == ray 24 | 25 | zero_slope = geometry.Segment(geometry.Point(0, 0), geometry.Point(1, 0)) 26 | expected_values( 27 | zero_slope, 28 | 0, 29 | 0, 30 | 1, 31 | 0, 32 | geometry.Ray(geometry.Point(0, 0), math.pi / 2), 33 | ) 34 | 35 | one_slope = geometry.Segment(geometry.Point(0, 0), geometry.Point(1, 1)) 36 | expected_values( 37 | one_slope, 38 | 0, 39 | 0, 40 | 1, 41 | 1, 42 | geometry.Ray(geometry.Point(0, 0), math.pi / 4), 43 | ) 44 | 45 | negative_one_slope = geometry.Segment(geometry.Point(0, 0), geometry.Point(1, -1)) 46 | expected_values( 47 | negative_one_slope, 48 | 0, 49 | -1, 50 | 1, 51 | 0, 52 | geometry.Ray(geometry.Point(0, 0), 3 * math.pi / 4), 53 | ) 54 | 55 | vertical_slope = geometry.Segment(geometry.Point(0, 0), geometry.Point(0, 1)) 56 | expected_values( 57 | vertical_slope, 58 | 0, 59 | 0, 60 | 0, 61 | 1, 62 | geometry.Ray(geometry.Point(0, 0), 0), 63 | ) 64 | 65 | 66 | def test_point_segment(): 67 | point_segment = geometry.Segment(geometry.Point(0, 0), geometry.Point(0, 0)) 68 | line_segment = geometry.Segment(geometry.Point(0, 0), geometry.Point(1, 2)) 69 | 70 | # make sure these do not throw 71 | _ = line_segment.to_ray() 72 | 73 | with pytest.raises(RuntimeError): 74 | _ = point_segment.to_ray() 75 | 76 | 77 | def test_ray_properties(): 78 | def expected_values(ray, segment): 79 | actual_segment = ray.to_segment() 80 | assert actual_segment.start.x == pytest.approx(segment.start.x) 81 | assert actual_segment.start.y == pytest.approx(segment.start.y) 82 | assert actual_segment.end.x == pytest.approx(segment.end.x) 83 | assert actual_segment.end.y == pytest.approx(segment.end.y) 84 | 85 | zero_angle = geometry.Ray(geometry.Point(0, 0), 0) 86 | expected_values( 87 | zero_angle, 88 | geometry.Segment( 89 | geometry.Point(0, 0), geometry.Point(0, geometry.DISTANT_POINT) 90 | ), 91 | ) 92 | 93 | forty_five_angle = geometry.Ray(geometry.Point(0, 0), math.pi / 4) 94 | expected_values( 95 | forty_five_angle, 96 | geometry.Segment( 97 | geometry.Point(0, 0), 98 | geometry.Point( 99 | math.sin(math.pi / 4) * geometry.DISTANT_POINT, 100 | math.cos(math.pi / 4) * geometry.DISTANT_POINT, 101 | ), 102 | ), 103 | ) 104 | 105 | right_angle = geometry.Ray(geometry.Point(0, 0), math.pi / 2) 106 | expected_values( 107 | right_angle, 108 | geometry.Segment( 109 | geometry.Point(0, 0), geometry.Point(geometry.DISTANT_POINT, 0) 110 | ), 111 | ) 112 | 113 | one_eighty_angle = geometry.Ray(geometry.Point(0, 0), math.pi) 114 | expected_values( 115 | one_eighty_angle, 116 | geometry.Segment( 117 | geometry.Point(0, 0), geometry.Point(0, -geometry.DISTANT_POINT) 118 | ), 119 | ) 120 | 121 | 122 | def test_ray_segment_round_trip(): 123 | def round_trip(ray: geometry.Ray): 124 | segment = ray.to_segment() 125 | new_ray = segment.to_ray() 126 | 127 | assert (ray.angle % (2 * math.pi)) == pytest.approx(new_ray.angle) 128 | assert ray.start == new_ray.start 129 | 130 | angle_0 = geometry.Ray(geometry.Point(0, 0), 0) 131 | angle_45 = geometry.Ray(geometry.Point(0, 0), math.pi / 4) 132 | angle_90 = geometry.Ray(geometry.Point(0, 0), math.pi / 2) 133 | angle_180 = geometry.Ray(geometry.Point(0, 0), math.pi) 134 | angle_270 = geometry.Ray(geometry.Point(0, 0), 3 * math.pi / 2) 135 | angle_405 = geometry.Ray(geometry.Point(0, 0), 2 * math.pi + math.pi / 4) 136 | 137 | round_trip(angle_0) 138 | round_trip(angle_45) 139 | round_trip(angle_90) 140 | round_trip(angle_180) 141 | round_trip(angle_270) 142 | round_trip(angle_405) 143 | 144 | 145 | def test_segment_intersections(): 146 | horizontal = geometry.Segment(geometry.Point(-1, 0), geometry.Point(1, 0)) 147 | vertical = geometry.Segment(geometry.Point(0, -1), geometry.Point(0, 1)) 148 | 149 | intersections = geometry.intersecting_segments(horizontal, [vertical]) 150 | 151 | assert len(intersections) == 1 152 | assert intersections[0][2] == vertical 153 | assert intersections[0][1].x == pytest.approx(0) 154 | assert intersections[0][1].y == pytest.approx(0) 155 | 156 | intersections = geometry.intersecting_segments(vertical, [horizontal]) 157 | 158 | assert len(intersections) == 1 159 | assert intersections[0][2] == horizontal 160 | assert intersections[0][1].x == pytest.approx(0) 161 | assert intersections[0][1].y == pytest.approx(0) 162 | 163 | 164 | def test_intersect_ray_to_perpendicular(): 165 | # vertical ray (x = 10) 166 | ray = geometry.Ray(geometry.Point(10, 5), math.pi) 167 | # horizontal segment (y = 0, x=[0, 20]) 168 | segment = geometry.Segment(geometry.Point(0, 0), geometry.Point(20, 0)) 169 | # they should intersect at (10, 0) 170 | intersections = geometry.intersect_ray(ray, [segment]) 171 | assert len(intersections) == 1 172 | assert intersections[0][1].x == pytest.approx(10) 173 | assert intersections[0][1].y == pytest.approx(0) 174 | 175 | # horizontal ray (y = 0) 176 | ray = geometry.Ray(geometry.Point(0, 0), math.pi / 2) 177 | # vertical segment (x = 4, y=[-10, 10]) 178 | segment = geometry.Segment(geometry.Point(4, -10), geometry.Point(4, 10)) 179 | # they should intersect at (4, 0) 180 | intersections = geometry.intersect_ray(ray, [segment]) 181 | assert len(intersections) == 1 182 | assert intersections[0][1].x == pytest.approx(4) 183 | assert intersections[0][1].y == pytest.approx(0) 184 | 185 | 186 | def test_intersect_ray_to_diagonal(): 187 | ray = geometry.Ray(geometry.Point(10, 5), math.pi) 188 | segment = geometry.Segment(geometry.Point(0, 0), geometry.Point(20, -20)) 189 | intersections = geometry.intersect_ray(ray, [segment]) 190 | assert len(intersections) == 1 191 | 192 | 193 | def test_camera_ray_intersections(): 194 | camera = raycasting.Camera(geometry.Point(10, 5), math.pi, math.pi / 4) 195 | segment = geometry.Segment(geometry.Point(0, 0), geometry.Point(20, 0)) 196 | segment2 = geometry.Segment(geometry.Point(0, 0), geometry.Point(40, -40)) 197 | 198 | for ray, point in camera.rays(10): 199 | intersections = geometry.intersect_ray(ray, [segment, segment2]) 200 | assert len(intersections) == 2 201 | -------------------------------------------------------------------------------- /raycasting.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import time 3 | from geometry import * 4 | 5 | 6 | class Camera: 7 | def __init__(self, location: Point, direction, viewing_angle): 8 | self.location = location 9 | self.direction = direction # angle from y-axis, "compass" style 10 | self.viewing_angle = viewing_angle 11 | self.planar_projection = True 12 | 13 | def try_move(self, distance, walls): 14 | new_location = self.location + Point( 15 | distance * math.sin(self.direction), distance * math.cos(self.direction) 16 | ) 17 | 18 | proposed_move = Segment(self.location, new_location) 19 | 20 | if len(intersecting_segments(proposed_move, walls)) == 0: 21 | # we don't intersect any wall, so we allow the move 22 | self.location = new_location 23 | 24 | def rotate(self, angle): 25 | self.direction = (self.direction + angle) % (2 * math.pi) 26 | 27 | def start_angle(self) -> float: 28 | return self.direction - self.viewing_angle / 2 29 | 30 | def end_angle(self) -> float: 31 | return self.start_angle() + self.viewing_angle 32 | 33 | def rays(self, count): 34 | # The idea is that we are creating a line 35 | # through which to draw the rays, so we get a more correct 36 | # (not curved) distribution of rays, but we still need 37 | # to do a height correction later to flatten it out 38 | 39 | start_angle = self.start_angle() 40 | end_angle = self.end_angle() 41 | 42 | if self.planar_projection: 43 | viewing_plane_start = self.location + Point( 44 | math.sin(start_angle), math.cos(start_angle) 45 | ) 46 | viewing_plane_end = self.location + Point( 47 | math.sin(end_angle), math.cos(end_angle) 48 | ) 49 | 50 | d_x = (viewing_plane_end.x - viewing_plane_start.x) / count 51 | d_y = (viewing_plane_end.y - viewing_plane_start.y) / count 52 | 53 | location = self.location 54 | 55 | for current in range(count): 56 | plane_point = Point( 57 | viewing_plane_start.x + (d_x * current), 58 | viewing_plane_start.y + (d_y * current), 59 | ) 60 | ray_segment = Segment(location, plane_point) 61 | 62 | yield ray_segment.to_ray(), plane_point 63 | else: 64 | angle_slice = self.viewing_angle / count 65 | 66 | for current in range(count): 67 | yield Ray( 68 | self.location, start_angle + current * angle_slice 69 | ), self.location 70 | 71 | 72 | def box(ul: Point): 73 | return [ 74 | Segment(ul + Point(0, 0), ul + Point(1, 0)), 75 | Segment(ul + Point(1, 0), ul + Point(1, -1)), 76 | Segment(ul + Point(0, 0), ul + Point(0, -1)), 77 | Segment(ul + Point(0, -1), ul + Point(1, -1)), 78 | ] 79 | 80 | 81 | def lr_triangle(ul: Point): 82 | return [ 83 | Segment(ul + Point(0, -1), ul + Point(1, -1)), 84 | Segment(ul + Point(1, 0), ul + Point(1, -1)), 85 | Segment(ul + Point(0, -1), ul + Point(1, 0)), 86 | ] 87 | 88 | 89 | def ur_triangle(ul: Point): 90 | return [ 91 | Segment(ul + Point(0, 0), ul + Point(1, 0)), 92 | Segment(ul + Point(1, 0), ul + Point(1, -1)), 93 | Segment(ul + Point(0, 0), ul + Point(1, -1)), 94 | ] 95 | 96 | 97 | def ll_triangle(ul: Point): 98 | return [ 99 | Segment(ul + Point(0, 0), ul + Point(1, -1)), 100 | Segment(ul + Point(0, -1), ul + Point(1, -1)), 101 | Segment(ul + Point(0, 0), ul + Point(0, -1)), 102 | ] 103 | 104 | 105 | def ul_triangle(ul: Point): 106 | return [ 107 | Segment(ul + Point(0, 0), ul + Point(1, 0)), 108 | Segment(ul + Point(1, 0), ul + Point(0, -1)), 109 | Segment(ul + Point(0, 0), ul + Point(0, -1)), 110 | ] 111 | 112 | 113 | def make_map(map_string): 114 | result = [] 115 | lines = map_string.split("\n") 116 | 117 | # start from top of map and work down 118 | y = len(lines) 119 | 120 | for line in lines: 121 | x = 0 122 | for char in line: 123 | if char == "#" or char == "*": 124 | result += box(Point(x, y)) 125 | if char == "/": 126 | result += ul_triangle(Point(x, y)) 127 | if char == "&": 128 | result += ur_triangle(Point(x, y)) 129 | if char == "%": 130 | result += lr_triangle(Point(x, y)) 131 | if char == "`": 132 | result += ll_triangle(Point(x, y)) 133 | 134 | x += 1 135 | y -= 1 136 | 137 | print(f"Segments: {len(result)}") 138 | 139 | # return result 140 | 141 | # if any segment exists twice, then it was between two map items 142 | # and both can be removed! 143 | result = [item for item in result if result.count(item) == 1] 144 | 145 | print(f"Filtered duplicated wall segments: {len(result)}") 146 | 147 | cont = True 148 | while cont: 149 | remove_list = [] 150 | for s in result: 151 | for n in result: 152 | if s.end.x == n.start.x and s.end.y == n.start.y and n.parallel(s): 153 | remove_list += [n, s] 154 | result.append( 155 | Segment(Point(s.start.x, s.start.y), Point(n.end.x, n.end.y)) 156 | ) 157 | break 158 | elif ( 159 | s is not n 160 | and s.end.x == n.end.x 161 | and s.end.y == n.end.y 162 | and s.parallel(n) 163 | ): 164 | remove_list += [n, s] 165 | result.append( 166 | Segment( 167 | Point(s.start.x, s.start.y), Point(n.start.x, n.start.y) 168 | ) 169 | ) 170 | break 171 | elif ( 172 | s is not n 173 | and s.start.x == n.start.x 174 | and s.start.y == n.start.y 175 | and s.parallel(n) 176 | ): 177 | remove_list += [n, s] 178 | result.append( 179 | Segment(Point(s.end.x, s.end.y), Point(n.end.x, n.end.y)) 180 | ) 181 | break 182 | 183 | if len(remove_list) > 0: 184 | break 185 | 186 | if len(remove_list) == 0: 187 | cont = False 188 | 189 | for i in remove_list: 190 | result.remove(i) 191 | 192 | print(f"Merged segments: {len(result)}") 193 | 194 | return result 195 | 196 | 197 | # 198 | # Symbols: 199 | # 200 | # / ### # or * ### & ### % # ` # 201 | # ## ### ## ## ## 202 | # # ### # ### ### 203 | # 204 | 205 | 206 | class Map2D: 207 | def __init__(self, width, height, scale): 208 | self.width = width 209 | self.height = height 210 | self.scale = scale 211 | self.center = Point(0, 0) 212 | 213 | def translate_and_scale(self, p: Point) -> Point: 214 | new_p = p - self.center 215 | new_x = new_p.x * self.scale 216 | new_y = self.height - new_p.y * self.scale 217 | return Point(new_x, new_y) + Point(self.width * 0.5, -self.height * 0.5) 218 | 219 | def draw_camera(self, surface, camera: Camera) -> None: 220 | pygame.draw.circle( 221 | surface, 222 | (0, 0, 255), 223 | self.translate_and_scale(camera.location), 224 | self.scale / 10, 225 | ) 226 | 227 | start_segment = Ray(camera.location, camera.start_angle()).to_segment(2) 228 | end_segment = Ray(camera.location, camera.end_angle()).to_segment(2) 229 | 230 | for segment in (start_segment, end_segment): 231 | pygame.draw.line( 232 | surface, 233 | (128, 128, 128), 234 | self.translate_and_scale(segment.start), 235 | self.translate_and_scale(segment.end), 236 | ) 237 | 238 | def draw_map(self, surface, segments: list[Segment]) -> None: 239 | for segment in segments: 240 | start = self.translate_and_scale(segment.start) 241 | end = self.translate_and_scale(segment.end) 242 | 243 | pygame.draw.line(surface, (255, 255, 255), start, end) 244 | 245 | 246 | def main(): 247 | game_map = """ 248 | ###########`&####### 249 | # ` / / # 250 | #/%#/&`&/&`& % `%`&# 251 | # / % / `/% & / # 252 | #& / ` & / & /%/%# 253 | # `& & `& ` `% ` &# 254 | # % # / `%& # `& # 255 | #% /% %`` / %/& & # 256 | #/% / &`%/ % /%& # 257 | # # //& %& %`& # 258 | # % %` %/ % &# 259 | #################### 260 | """ 261 | 262 | map_wall_segments = make_map(game_map) 263 | 264 | pygame.init() 265 | 266 | width = 1280 267 | height = 480 268 | 269 | map2d = Map2D(height / 3, height / 3, 30) 270 | screen = pygame.display.set_mode((width, height)) 271 | 272 | FOV = 2 * math.atan((width / 800) * math.tan((math.pi / 2) / 2)) 273 | 274 | camera = Camera(Point(-0.5, -0.5), math.pi / 2, FOV) 275 | 276 | frame = 0 277 | last_time = time.perf_counter() 278 | 279 | fisheye_distance_correction = True 280 | minimap_on = True 281 | 282 | while True: 283 | pygame.display.get_surface().fill((0, 0, 0)) 284 | 285 | frame += 1 286 | 287 | if frame % 10 == 0: 288 | new_time = time.perf_counter() 289 | elapsed, last_time = new_time - last_time, new_time 290 | 291 | print( 292 | f"{10 / elapsed} fps ({camera.location.x},{camera.location.y}) {camera.direction}" 293 | ) 294 | 295 | for event in pygame.event.get(): 296 | if event.type == pygame.QUIT: 297 | pygame.quit() 298 | if event.type == pygame.KEYDOWN: 299 | if event.key == pygame.K_1: 300 | camera.planar_projection = not camera.planar_projection 301 | if event.key == pygame.K_2: 302 | fisheye_distance_correction = not fisheye_distance_correction 303 | if event.key == pygame.K_m: 304 | minimap_on = not minimap_on 305 | 306 | keys = pygame.key.get_pressed() 307 | 308 | if keys[pygame.K_UP]: 309 | camera.try_move(0.08, map_wall_segments) 310 | if keys[pygame.K_DOWN]: 311 | camera.try_move(-0.08, map_wall_segments) 312 | if keys[pygame.K_RIGHT]: 313 | camera.rotate(math.pi / 60) 314 | if keys[pygame.K_LEFT]: 315 | camera.rotate(-math.pi / 60) 316 | 317 | col = 0 318 | 319 | last_match = None 320 | last_wall = None 321 | 322 | for r, segment_point in camera.rays(width): 323 | matches = intersect_ray(r, map_wall_segments) 324 | 325 | def sort_criteria(line): 326 | return line[0] 327 | 328 | # sort by closest, and draw it 329 | matches.sort(key=sort_criteria, reverse=False) 330 | 331 | # only draw the closest wall. 332 | if len(matches) > 0 and matches[0][0] != 0: 333 | distance_from_eye = matches[0][0] 334 | 335 | # Distance correction from https://gamedev.stackexchange.com/questions/45295/raycasting-fisheye-effect-question 336 | corrected_distance = ( 337 | distance_from_eye * math.cos(camera.direction - r.angle) 338 | if fisheye_distance_correction 339 | else distance_from_eye 340 | ) 341 | 342 | wall_height = (height * 0.75) / corrected_distance 343 | if wall_height > height: 344 | wall_height = height + 2 345 | 346 | wall_start = (height - wall_height) / 2 347 | wall_end = wall_start + wall_height 348 | 349 | # Draw edge if detected 350 | if last_match is not matches[0][2] and col != 0: 351 | if last_match is None: 352 | pygame.draw.line( 353 | pygame.display.get_surface(), 354 | (255, 255, 255), 355 | (col, wall_start), 356 | (col, wall_end), 357 | ) 358 | else: 359 | pygame.draw.line( 360 | pygame.display.get_surface(), 361 | (255, 255, 255), 362 | (col, min(wall_start, last_wall[0])), 363 | (col, max(wall_end, last_wall[1])), 364 | ) 365 | else: 366 | # draw just top and bottom points otherwise 367 | screen.set_at((col, int(wall_start)), (255, 255, 255)) 368 | screen.set_at((col, int(wall_end)), (255, 255, 255)) 369 | 370 | # and some texture... 371 | texture_size = int(height / 50) 372 | if col % texture_size == 0: 373 | for y in range(int(wall_start), int(wall_end), texture_size): 374 | screen.set_at((col, y), (255, 255, 255)) 375 | 376 | last_wall = (wall_start, wall_end) 377 | last_match = matches[0][2] 378 | else: 379 | # Look for transition from wall to empty space, draw edge 380 | if last_match is not None: 381 | pygame.draw.line( 382 | pygame.display.get_surface(), 383 | (255, 255, 255), 384 | (col, last_wall[0]), 385 | (col, last_wall[1]), 386 | ) 387 | last_match = None 388 | 389 | col += 1 390 | 391 | if minimap_on: 392 | map_surface = pygame.Surface((map2d.width, map2d.height)) 393 | map2d.center = camera.location 394 | map2d.draw_map(map_surface, map_wall_segments) 395 | map2d.draw_camera(map_surface, camera) 396 | pygame.display.get_surface().blit( 397 | map_surface, (width - map2d.width, height - map2d.height) 398 | ) 399 | 400 | pygame.display.flip() 401 | 402 | 403 | if __name__ == "__main__": 404 | main() 405 | --------------------------------------------------------------------------------