ScratchData LogoScratchData
Back to ChromeCat_test's profile

Point in rotated rectangle test

CHChromeCat_test•Created May 2, 2021
Point in rotated rectangle test
11
8
85 views
View on Scratch

Instructions

Click anywhere on the screen to test if that point lies within the rectangle. This is a simple point in rect test I made to use in my larger projects. It uses an affine transformation which is the fastest and shortest way to do the test. (One huge advantage is you can store the transformation matrix and angles in lists to reuse which cuts down on calculation time) --How it works-- Given a rectangle defined by p1, p2 and width w: 1. The angle between the two points defining the rectangle is found, using the formula for atan2. 2. The test point is then transformed first by a translation of -p1 then by rotated by -θ (inverse affine transformation) 3. The resulting point is tested to see if it is within a non-rotated rectangle with p1 at (0,0), width w and length equal to the magnitude of the vector p2-p1. Which is a much simpler test to do.

Description

If you want to use this in your own project, place both the "pt_in_rect_test" "inverse_affine" custom blocks in your backpack. It uses 3 variables x',y' and pt_in_rect.

Project Details

Project ID524384366
CreatedMay 2, 2021
Last ModifiedNovember 30, 2021
SharedMay 2, 2021
Visibilityvisible
CommentsAllowed