Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to test if a line segment intersects a square
- minekraftkid
-
Scratcher
100+ posts
How to test if a line segment intersects a square
I have a square with fixed length h, and a line segment that consists of two ordered pairs. I’m trying to detect if the line intersects with the square. I was able to hash out a solution, but it’s extremely expensive to run. “Node X” and “Node Y” contain x and y coordinates of points, while “Connects From” and “Connects To” are lists that contain the index of entires in the node x and node y, which makes the lines between points. Calculate Line Data runs at the start of the project, because the values of the lines do not change during runtime, while the Detect Player runs every tick. The variable “Touching Line?” is set to 1 or 0 depending on if the player is touching one of the lines.
The script is below, and I’ve attached a desmos project that shows the transformations that are done here.

While this works (Although it also rotates the players square hitbox, which isn’t wanted), it’s extremely expensive to run. Does anybody have a better way of doing this? There’s bound to be a much easier method, but I’m stumped on how to simplify this.
Edit: The line segment being contained inside the square hitbox should count as an intersection.
The script is below, and I’ve attached a desmos project that shows the transformations that are done here.

While this works (Although it also rotates the players square hitbox, which isn’t wanted), it’s extremely expensive to run. Does anybody have a better way of doing this? There’s bound to be a much easier method, but I’m stumped on how to simplify this.
Edit: The line segment being contained inside the square hitbox should count as an intersection.
Last edited by minekraftkid (Feb. 28, 2023 15:12:04)
- Woodfur
-
Scratcher
100+ posts
How to test if a line segment intersects a square
Why not just instantiate the lines as actual sprites and use the
<touching [line v] ?>block? It's pretty much what Scratch is designed for.
- legendary34678
-
Scratcher
1000+ posts
How to test if a line segment intersects a square
I believe you could calculate this using just the coordinates alone. Here's line segment intersection in Desmos that I quickly whipped up, which I think can easily be carried over to Scratch.
Last edited by legendary34678 (Feb. 28, 2023 07:01:31)
- minekraftkid
-
Scratcher
100+ posts
How to test if a line segment intersects a square
Why not just instantiate the lines as actual sprites and use a sensing block? It's pretty much what Scratch is designed for.First of all, speed. Project needed to work with more pieces than the 200 clone limit, which was already slow with lower clone amounts. Additionally, I’m using the nodes for pathfinding for NPC’s, which would mean using sensing blocks would require building out the data from the lists each frame, which also is slow.
I believe you could calculate this using just the coordinates alone. Here's line segment intersection in Desmos that I quickly whipped up, which I think can easily be carried over to Scratch.This is exactly what I was looking for, thank you.
- ventoaurum
-
Scratcher
100+ posts
How to test if a line segment intersects a square
isn't that like.. basic geometry? no offense tho
- minekraftkid
-
Scratcher
100+ posts
How to test if a line segment intersects a square
isn't that like.. basic geometry? no offense thoYeah I think so. I’ve found that the farther I go into math, the more I forget seemingly simple concepts. I can do Laplace transformations, but basic algebra is hard. That’s what’s makes the forums nice. Someone’s bound remember the concepts I’ve forgotten.
That being said, if you have a more efficient way to detect the intersection, I would love to hear it.
- ventoaurum
-
Scratcher
100+ posts
How to test if a line segment intersects a square
happens to everyone bro, no shaming or anything like that. And no, I'm pretty sure legendary34678' way is the only/simplest way, I'll say it if I find a better way tho.isn't that like.. basic geometry? no offense thoYeah I think so. I’ve found that the farther I go into math, the more I forget seemingly simple concepts. I can do Laplace transformations, but basic algebra is hard. That’s what’s makes the forums nice. Someone’s bound remember the concepts I’ve forgotten.
That being said, if you have a more efficient way to detect the intersection, I would love to hear it.
- pkhead
-
Scratcher
1000+ posts
How to test if a line segment intersects a square
ooh, collision
i thought of something, did you try something like this?

what this sort of depicts is taking the dot product of every point on the square and the line/plane normal vector. if one of the dot products are zero or negative, there is an intersection. since you're talking about line segments, you should also take the dot products between each point of the square and the line direction vector, and check if it's within the bounds of the line segment.
perhaps if this works it could be more efficient
i thought of something, did you try something like this?

what this sort of depicts is taking the dot product of every point on the square and the line/plane normal vector. if one of the dot products are zero or negative, there is an intersection. since you're talking about line segments, you should also take the dot products between each point of the square and the line direction vector, and check if it's within the bounds of the line segment.
perhaps if this works it could be more efficient
- Discussion Forums
- » Help with Scripts
-
» How to test if a line segment intersects a square