0

I have a grid with a path running through it. Within each of the grid squares, where the path intersects, I want to determine which side of the current grid has the smaller area. I was hoping there was a clever way to do this by measuring up (or down) from the grid corners, to where the path intersects, and calculating if they crossed more than 50% of the grid, but I'm not clever enough cuz whatever I try fails. So I was wondering if anyone could suggest the proper route to do it this for each square will have a different shape.

I'm assuming I'll have to break each grid into "shapes", and then calculate the area of the shapes; but I was hoping not to have to go that route. In my case I'm writing a javascript program to automate this, so having to determine the shapes programmatically will also be a challenge. Thanks!

Here's a pic of what I'm talking about.

enter image description here

Edit: BTW I do know the x,y positions of all path intersections and I know the grid points.

Valerie
  • 115

2 Answers2

2

@Valerie:

Does this figure help?

enter image description here

Just take the average of the $y$ (vertical) values at the left value of $x$ and right value of $x$--the green points. If this average is above the squares midpoint, then the larger area is below.

1

You don't have to measure anything.

The straight line is given by an equation of the form $$ ax + by = c. $$ If you use the center of a grid square as $(x,y)$ you will have an inequality (unless the center lies on the line).
$$ ax + by > \text{ or } < c. $$ When "$>$" the center is above the line and so more than half the area is above the line.

Ethan Bolker
  • 95,224
  • 7
  • 108
  • 199
  • This sounds super easy, but I don't see it in my head. Thanks for the quick reply, I'll plug in some numbers and figure it out. Will mark as answer too. Thanks. – Valerie Jul 27 '21 at 22:04
  • I've got the center x,y but where'd a and b come from. Is that the area of the two parts of the grid? – Valerie Jul 27 '21 at 22:27
  • $a$ and $b$ and $c$ are the numbers you need to specify the line. – Ethan Bolker Jul 27 '21 at 22:30
  • Oh okay. Well I have the 4 corners and now the center, as well as the points x(t) and y(t) where the path intersects, but this is a bezier curve so I really don't have the line. – Valerie Jul 27 '21 at 22:32
  • If you have any two of the points of intersection with the grid then you can find the equation of the line. If it's really a straight line then I don't see how Bezier curves come in. – Ethan Bolker Jul 27 '21 at 22:39
  • Thanks Ethan. The bezier is the entire line, but for purposes of the grid, we can just say the line is linear. So I found the change in Y/change in X is my slope. So is the slope now "c". Jeezus I feel dumb. I'm just hammering this in without learning but I only want to program :) – Valerie Jul 27 '21 at 22:48
  • No. The slope is $-a/b$. You can just say your change in $y$ over change in $x$ is $a$ and set $b=1$. To find $c$ plug in $(x,y)$ for any point you know is on the line and solve for $c$.If you look up "linear equation" in something like Khan Academy your programming will go much faster. – Ethan Bolker Jul 28 '21 at 01:00