(Updated: 2023-11-25)
1. How to find the intersection between a line and geometrical objects
- Important things that you should know:
1) Some objects that have area should be changed into polylines by using RegionBoundary
2) To find the intersection, you could use the following ways in the Mathematica;
• Solve
function
• RegionIntersection
function
• `Graphics'Mesh'FindIntersections[]` function [3]
• User-defined function (such as a function using the bisection method)
Here, we discuss only the first and second cases. Let's look at the methods with examples;
Example 1: Let's obtain an intersection made by two objects: a square with a length of 3, which is rotated by 40 degrees in the anti-clockwise direction, and a line in which its initial point is at the origin and the point ends at (5,0).
(*Define a line*)
p1 = {0, 0}; p2 = {5, 0};
line = Line[{p1, p2}];
(*Define a square rotated by 40 degree*)
length = 3.0;
square = GeometricTransformation[
Rectangle[{-0.5 * length, -0.5 * length}, {0.5 * length,
0.5 * length}],
RotationTransform[40 Degree]
];
square = RegionBoundary@square; (*Apply RegionBoundary to get the boundary*)
intersection = Values@Solve[{x, y} \[Element] line && {x, y} \[Element] square, {x, y}];
(*Graphical result*)
Graphics[{
Red, line, Blue, square,
Black, PointSize[0.015], Point[intersection]
},
Frame -> True,
Axes -> True]
Note: you could obtain the same result using RegionIntersection
, which return an object such as point.
>>> RegionIntersection[line, square]
Point[{1.95811, 0.}]
However, when you consider the computational time, RegionIntersection
is ten times slower than Solve
, as below;
>>> Values@Solve[{x, y} \[Element] line && {x, y} \[Element] square, {x, y}] // AbsoluteTiming
>>> RegionIntersection[line, square] // AbsoluteTiming
{0.0967799, {{1.95811, 0}}}
{0.951184, Point[{{1.95811, 3.60822*10^-16}}]}
Therefore, it would be a wise way to use Solve
.
Update: I found that there is another option to find intersections in Mathematica, which is an undocumented function, ` Graphics'Mesh'FindIntersections[]`. This function gives results much faster than the above two options;
>>> Graphics`Mesh`FindIntersections[{line, square}, Graphics`Mesh`AllPoints -> False] // AbsoluteTiming
{0.0036275, {{1.95811, 0.}}}
Reference
[1] https://mathematica.stackexchange.com/questions/195297/solve-line-and-rectangle-intersections
[2] https://mathematica.stackexchange.com/questions/69168/how-to-find-the-intersection-of-a-line-with-the-boundary-of-a-region
[3] https://mathematica.stackexchange.com/questions/164078/what-is-the-fastest-way-to-compute-intersection-of-infinite-lines-with-a-given-b
'프로그래밍 언어 > Mathematica' 카테고리의 다른 글
[Mathemaitca] 코드 작성 시 유용한 약어들 (0) | 2023.11.23 |
---|---|
[Mathematica] Convert E-notation (C-language, Fortran) to numbers in Mathematica (0) | 2023.11.19 |
[Mathamatica] Mathamatica 무료로 이용하기: Wolfram Engine (0) | 2022.03.27 |
[Mathematica] Jupyter 노트북으로 Mathematica 사용하기 (0) | 2022.03.27 |
[Mathematica] Linux terminal에서 프로그램 실행 (0) | 2021.03.18 |
댓글