News for hackers. Stuff that matters.
Re:Breaking down Amazon's mega dropdown by slig on Wednesday March 06, 2013 @09:44AM (#5331350)
Really nice! Two questions:

- What's the "cross-product magic Amazon uses to detect movement inside the 'blue triangle.'"?

- How did you do those GIFs?

Re:Breaking down Amazon's mega dropdown by moron4hire on Wednesday March 06, 2013 @09:52AM (#5331395)
It's not magic, it's just math. The cross product [1] between the vector of the mouse movement and the vector of the mouse's location to the menu's top left corner tells you the relative orientation of the two vectors. Compare that to cross product in regards to the menu's bottom left corner and you can easily figure out that the mouse must be moving towards the menu. This is much easier than figuring out the bounding triangle that the person drew in this article and then figuring out if the mouse is inside of it.

[1] http://en.wikipedia.org/wiki/Cross_product

Re:Breaking down Amazon's mega dropdown by Dn_Ab on Wednesday March 06, 2013 @10:28AM (#5331588)
Are you sure about that? The dot product gives a measure of the orientation between two vectors, the cross product gives perpendicularity and is vector valued (kind of, the distinction matters more for physics than graphics). It also only makes sense in 3 and 7 dimensions without generalizing the concept of vector.
Re:Breaking down Amazon's mega dropdown by moron4hire on Wednesday March 06, 2013 @11:08AM (#5331822)
Balls, you are correct, dot product is the correct operation, not cross product. Been a while since graphics ops were on my table.

EDIT: Well, cross product would work, too, if you extended the 2D vectors into 3D vectors, coplanar in the screen. The orientation of the cross product into or out of the screen would also tell you the relative orientation of the two vectors. So then, you'd be looking for the two cross products with differing signs on the Z component to tell you if it's "in the triangle".

Re:Breaking down Amazon's mega dropdown by moron4hire on Wednesday March 06, 2013 @03:05PM (#5333457)
After some playing around, dot product won't do it. You can get close with dot product, but it really only gives you the general direction. Cross product (and really, only calculating the Z component of the cross product of coplanar 3D vectors) will get it exactly right every time.

So basically, for a moving between two mouse points (Ix, Iy) and (Ax, Ay), and a top left menu corner (Bx, By) and bottom left (Cx, Cy), then:

DAx = Ax - Ix

DAy = Ay - Iy

DBx = Bx - Ix

DBy = By - Iy

DCx = Cx - Ix

DCy = Cy - Iy

Z1 = DBx * DAy - DAx * DBy

Z2 = DCx * DAy - DAx * DCy

Then, you know you're headed towards that menu if Z1 < 0 and Z2 > 0.

Could be simplified a little if you assume the X component of the top left corner of the menu will be the same as the X component of the bottom left corner, but the math was clearer to keep them separate.

Re:Breaking down Amazon's mega dropdown by moron4hire on Wednesday March 06, 2013 @03:29PM (#5333644)
And if you have any convex polygon as an ordered, circular list of points proceeding clockwise, then you can easily loop around the points checking your cross product values making sure they are all greater than 0 to determine if the point is in the polygon. You'll have to tessalate concave polygons into a list of convex polygons, but that's eaaaaaasy ;)
Re:Breaking down Amazon's mega dropdown by Dn_Ab on Wednesday March 06, 2013 @11:41AM (#5332039)
Right and excellent explanation. I was more disagreeing with your definition than saying it could not be done with "cross products" (although it's still bad terminology, biased to people that already know the concepts). That's why I couldn't say you were wrong. Intuitively, a measure of how parallel two vectors of arbitrary dimensions are, is a better default when talking about orientation of vectors.
Re:Breaking down Amazon's mega dropdown by gbhn on Wednesday March 06, 2013 @12:17PM (#5332271)
You need cross product if you want to know sign. Dot product won't tell you which side of the line you're on, and that sounded like a big part of the algorithm.
Re:Breaking down Amazon's mega dropdown by munchbunny on Wednesday March 06, 2013 @05:08PM (#5334355)
No, the dot product won't work in this case. Take the two edges of the triangle as vectors. You want the mouse vector to fall in between. The dot product can't tell you which side of the two edge vectors your mouse vector falls on, but the cross product can if you look at the component of the cross product that points in/out of the screen.

Actually, you can make the dot product give you that answer, but you would need use the dot product of the mouse vector and two vectors normal to the edge vectors, which is really just a roundabout way of using the cross product.

Re:Breaking down Amazon's mega dropdown by bjterry on Wednesday March 06, 2013 @09:49AM (#5331384)
I just happen to have this open in another tab, but only helpful if you are a Mac user: https://gist.github.com/dergachev/4627207
Re:Breaking down Amazon's mega dropdown by slig on Wednesday March 06, 2013 @11:06AM (#5331812)
Thank you! In the gist comments I discovered another nice product that does something very similar using canvas: Phosphor - http://news.ycombinator.com/item?id=5137028
Re:Breaking down Amazon's mega dropdown by glomph on Wednesday March 06, 2013 @11:57AM (#5332144)
https://code.google.com/p/sharex/ makes those types of gifs very easy on windows. It can also do a load of other cool stuff.
Re:Breaking down Amazon's mega dropdown by jchavannes on Wednesday March 06, 2013 @11:19PM (#5336054)
Off topic, but the Reddit post about Neox Screen using the ShareX source code and asking for donations is quite disheartening. Not that anything illegal occurred, just the level of deceitfulness.

http://www.reddit.com/r/opensource/comments/17qmek/my_friend...

Re:Breaking down Amazon's mega dropdown by duncanj on Thursday March 07, 2013 @06:37PM (#5341139)
Once, I put a broken treadmill on the curb with a sign that said "Free." I came back a while later and it had a sign that said "$20".
Re:Breaking down Amazon's mega dropdown by kamens on Wednesday March 06, 2013 @07:03PM (#5334963)
Since others answered the harder question: camtasia+gifbrewery
Re:Breaking down Amazon's mega dropdown by bazzargh on Wednesday March 06, 2013 @10:48AM (#5331713)
I'm guessing that the cross product magic was a hit test for a triangle. Here's an explanation:

http://www.blackpawn.com/texts/pointinpoly/default.html

Re:Breaking down Amazon's mega dropdown by jamesmiller5 on Wednesday March 06, 2013 @11:31AM (#5331979)
Here are a few different solutions to determine if a point is in a polygon with code examples. I think solution 3 might be applicable and faster.

Section: Determining if a point lies on the interior of a polygon http://paulbourke.net/geometry/polygonmesh/

Re:Breaking down Amazon's mega dropdown by bazzargh on Wednesday March 06, 2013 @12:45PM (#5332481)
Yup, that looks right, but it can be made even simpler in this case because one of the segments is always vertical.

  var prevOffset = prev.x-lowerRight.x;
  loc.x < lowerRight.x
  && (loc.y-prev.y)*-prevOffset-(loc.x-prev.x)*(upperRight.y-prev.y) <0
 && (loc.y-lowerRight.y)*prevOffset-(loc.x-lowerRight.x)*(prev.y-lowerRight.y) < 0
This is in fact equivalent to what's in jQuery.menu-aim.js, eg upperSlope < prevUpperSlope => upperSlope - prevUpperSlope < 0, then inline slope() and multiply through by the denominators.

I'd prefer the code above though, as there's no icky division by zero.

Re:Breaking down Amazon's mega dropdown by slig on Wednesday March 06, 2013 @11:07AM (#5331817)
Got it, thanks! Should've thought about googling "hit test triangle".