439 views

# Scaling vectors inside a Canvas

1. last year

### Armando S

17 Feb 2017 Parma, Italy

Hello,
I need to draw some vectors (defining different speeds in Km/h) inside a Canvas. If I use the speed value (module) of the vectors, their length exceeds the limits of the Canvas (width and height) so I need to scale them maintaining the relative proportions (i.e. if vector A is twice vector B, it must stay so in the drawing as well).
I really have no idea how to do it. I found a Xojo example (it is called Newton27, don't remember where I got it) which scales vector but it is very difficult to follow the process because it jumps through different methods and properties.
I'm looking for something fairly simple, if someone has already tackled the same problem in his/her coding.

Thank you

See post in context

You'll need to iterate through all your vectors and find the largest X and Y values. Then your scale factors are

maxX / canvas.Width
maxY / canvas.Height

Note that this could distort your image. If that isn't acceptable, then take the smaller scale factor (assuming they're less than 1) and use that for both X and Y.

2. ### Markus W

17 Feb 2017 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...

@Armando S I found a Xojo example (it is called Newton27, don't remember where I got it)

Would love to have a look at that. Can you set up a link?

3. ### Armando S

17 Feb 2017 Parma, Italy

No problem. It should be this link:
https://db.tt/PXlWbTdlNR
(if it does not work let me know, I'm no great Dropbox expert)

4. ### Markus W

17 Feb 2017 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...

Thanks.

Well, if all you need to do is zoom in and out then simply replace each x and y with x*ZoomFactor and y*ZoomFactor respectively.

Look at the CanvasZoom example included with Xojo.

5. ### Armando S

17 Feb 2017 Parma, Italy

Maybe I was unclear: what I need to do is scaling whatever vector to fit the Canvas. I have to figure out the scaling factor.