sdasdadas
sdasdadas

Reputation: 25096

How do I translate and scale points within a bounding box?

I have a number of points P of the form (x, y) where x,y are real numbers. I want to translate and scale all these points within a bounding box (rectangle) which begins at the point (0,0) (top left) and extends to the point (1000, 1000) (bottom right).

Why is it that the following algorithm does not produce points in that bounding box?

for Point p in P:
    max = greatest(p.x, p.y, max)

scale = 1000 / max
for Point p in P:
    p.x = (p.x - 500) * scale + 500
    p.y = (p.y - 500) * scale + 500

I fear that this won't work when p.x or p.y is a negative number.

I would also like to maintain the "shape" of the points.

Upvotes: 0

Views: 1257

Answers (1)

Pieter Geerkens
Pieter Geerkens

Reputation: 11883

  1. Find all of yMin, yMax, xMin, xMax, xDelta = xMax-xMin and yDelta = yMax-yMin for your set of points.
  2. Set max = greatest(xDelta,yDelta).
  3. Foreach Point p set p.X = (p.X - xMin) * scale and p.Y = (p.Y - yMin) * scale

Upvotes: 2

Related Questions