Khan / Prototope

Swift library of lightweight interfaces for prototyping, bridged to JS
http://khan.github.io/Prototope
230 stars 18 forks source link

Fixed Rect.center setter #113

Closed NachoSoto closed 8 years ago

NachoSoto commented 8 years ago

Notice while inspecting the code.

sophiebits commented 8 years ago

I think this is correct as-is? If you do rect.center = rect.center, it should add 0, not set to 0.

NachoSoto commented 8 years ago

Hmm I'm not sure I understand. If you're setting the center of a Rect to a new value, the existing origin shouldn't matter?

sophiebits commented 8 years ago

If you have a 2x2 box with origin at (3, 4) then its center is at (4, 5). If I do .center = (4, 6) it should move over 1 unit. One way to do that is to adding (0, 1) to the origin, which you can calculate by subtracting (4, 6) – (4, 5).

Your suggested code does origin = Point(4, 6) - Point(4, 5) which sets the origin to (0, 1).

sophiebits commented 8 years ago
origin += newCenter - oldCenter
origin = oldOrigin + (newCenter - oldCenter)

is equivalent to

origin = newCenter + (oldCenter - oldOrigin)

if that helps you think about it, where oldCenter - oldOrigin is a vector pointing from the origin to the center (half the box's size in each dimension, invariant of position).

andymatuschak commented 8 years ago

(thanks, @spicyj—that is indeed what I intended!)

jbrennan commented 8 years ago

(just a meta note: boy is this conversation hard to follow with only symbolic math; I wish there was an easy way for us to discuss (or even program!) these things geometrically)

NachoSoto commented 8 years ago

Ooooh that makes a lot of sense, thanks for the explanation @spicyj. I don't know why I found it so unintuitive at first!