Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Math help: Trapezium/trapezoid coordinates to prim parameters?

Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-11-2007 19:01
Any coordinate geometry wizards here? :)

Looks like American and British math use these terms differently (according to Wikipedia) :) Anyway, I'm talking about the shape that has 4 sides, 2 of which are parallel. Assuming it's a size/shape that can be built with a prim (i.e. not stretched/skewed beyond what's allowed), can someone help me with the math here?

Let's say the points are defined by (x1, y1), (x2, y2), ... (x4, y4). And let's say I know that edges 1-2 and 3-4 are parallel, and 1-2 is bigger than 3-4. So:

CODE

4 3



1 2


I drew that horizontal, it could be rotated of course.

So, some things are easy:

* The size of the base edge of the prim is distance between 1 and 2, i.e. llVecMag(llVecDist(1, 2))

* The opposite parallel edge needs to be defined by the 'top size' parameter, which is a ratio, right. So that's the size of the 3-4 edge divided by the size of the 1-2 edge.

Then I need to figure out the skew, because the 3-4 edge won't be centered on the 1-2 edge. Also, I need to figure out the position/rotation of the whole prim so that the corners line up with the coordinates.

Thanks,
Ziggy
Ed Gobo
ed44's alt
Join date: 20 Jun 2006
Posts: 220
03-11-2007 22:08
Just applied taper and top shear to a prim to see if I could see any relationship, and, sorry, I don't see any obvious relationship. But applying them does keep 12 // 34.

If you can solve this visually, that would be the way to do it, maybe in a large prim, then shrink it?
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
03-12-2007 09:47
This looks fun... let's see. You've solved for top size and the scale along the 12 direction easily enough. What's left is the scale perpendicular to that and the skew, then the position and rotation, right?

The scale perpendicular is easy enough. I'll assume that all four points are coplanar. I'll make a vector going from point 4 to point 1, and also a unit vector in the direction of the base:

CODE

vector p4top1 = <x4, y4, 0> - <x1, y1, 0>;
vector p2top1 = llVecNorm(<x2, y2, 0> - <x1, y1, 0>);


Now I need to subtract off the portion of that vector that's in the direction of 1-2. Remember that the dot product gives you the amount of one vector that is in the direction of another:

CODE

vector minor_axis = p4top1 - p2top1 * (p4top1 * p2top1);
float minor_scale = llVecMag(minor_axis);


First that found out how much of that diagonal vector was in the direction of the base by calculating the dot product, which is a number. Then it turned that back into a vector by multiplying it by the direction of the base again. Finally, this vector was subtracted from the diagonal, which should result in just a vector along the perpendicular ("minor") axis. The llVecMag() of this is the smaller scale value you need.

Now I think I'll skip the skew for a moment and calculate the prim-center. I'm going to assume this thing's completely flat for a moment.

To get to the center, I'll need to start at one side of the base, move halfway along that line, then turn 90 degrees and go halfway up toward the "top". This should do that:

CODE

minor_axis = llVecNorm(minor_axis);
vector center = <x2, y2, 0> + p2top1 * major_scale * 0.5 - minor_axis * minor_scale * 0.5;


I got into a little bit of trouble because my minor axis was pointing from the top to the bottom, so I ended up having to subtract rather than add.

Now, I'm not completely clear on how skews work because I'm not in SL, but I believe that a 50% sckew would put p4 directly over the center. A 0 skew should put the center of 4-3 right over the center of the prim. Let's see how far off those two centers actually are.

CODE

vector minor_center = (<x3, y3, 0> + <x4, y4, 0>)/2;
float skew_amount = llFabs((minor_center - center)*p2top1);


Now, I believe, if you take skew_amount and divide it by the length of the base, you'll get either the skew value, or twice the skew value.

Rotation is a little trickier. What you're going to need to do is use llAxes2Rot(). You'll need to figure out which way a trapezoid prim is oriented in SL, and figure out which of the unit vectors I wrote above corresponds to the local axes of the prim. I THINK this should do it:

CODE

vector localZ = -minor_axis;
vector localX = p2top1;
vector localY = localZ % localX;
rotation rot = llAxes2Rot(localX, localY, localZ);


I knew that what I've been calling the "minor axis" is the local Z for a top-sized prim. I chose to make the base the X axis, and that nailed down where the Y axis goes. Since SL uses a "right-hand-rule" coordinate system, I was able to figure out which direction each axis would need to be in.

I hope all this helped... I just sort of spewed this out and I kinda just woke up ;) The fun part about all this is that I didn't once have to use a trig function. Proper use of vectors and rotation math can almost always avoid the need to use trig functions.
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-12-2007 12:58
Wow. Thanks a lot :)

From: someone
Remember that the dot product gives you the amount of one vector that is in the direction of another


That is exactly the kind of stuff I did not remember :)

I'll play with these and see where I get. But maybe I'm approaching this wrong, so you might be able to help me there as well. I'm basically trying to make a large faceted spheroid. I started with Cadroe Murphy's Shapegen, which of course tiles a ring/sphere perfectly. I tried to edit that to handle ellipses (when making rings) and spheroids. I got a rough approximation... if I use a large number of prims, then it looks OK, but that math is obviously not right.

I was trying to solve it the way he'd solved the sphere case. But changing from a single radius to different X/Y radii was a little difficult. Also, I realized that to get the inner edge to tile perfectly, I'll need to skew the prim, which isn't necessary for circles/spheres. So... I thought I'd try a different approach, which led me to this trapezoid thing. But if you can think of a better way of solving the spheroid problem, that would be extremely cool :)

Anyway, my thinking so far:

An ellipse is defined by:

x = a cos t
y = b sin t

So... take 360 degrees and divide it by number of prims. That gives me the angle subtended by each piece. Loop angle = 0 to 360, in these step sizes. (So far, same as Shapegen)

Now, at each angle, I can figure out the x,y coords of the point on the ellipse. So to get the edge of the facet, I get the coords for angle - delta and angle + delta, where delta is 1/2 the subtended angle. I hope this is making sense :) Anyway, those 2 points are the 1,2 points of my trapezoid (the bigger parallel edge). For the smaller parallel edge, the angles stay the same, and use a smaller radius to calculate x,y.

So that gets me 4 coordinates, then I had to figure out how to place the prim so it tiled.

And all of this is for the elliptical ring only... I haven't even started to think about how to handle the 3D case, where each prim has to stretch differently in the 3rd axis, and tilt differently as well.
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
03-12-2007 13:50
I haven't looked at the details, but Seifert Surface wrote a very cool sounding set of scripts that linked up multiple prims (all in a flat plane) into a continuous path.

From my understanding, you would put down rectangular plates, drop the script in each prim, and the prims would stretch to connect smoothly.

I am almost certain that these stretched shapes would have to be trapezoids.

Might want to try looking for this?
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-12-2007 13:59
Yes, I remember that thread. I think I looked for it, but I'll look for it again. Wasn't that where he was trying to do Beizier (sp) curves by defining control points?

That might be an easier way to get a spheroid. I should be able to find an equation that gives me the equation for the surface of a spheroid, then I could generate a set of points by calculating the x,y,z, values for a looping parameter set (basically, rotating the angle). Thanks for that tip.
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
03-12-2007 16:55
Path Maker:

/54/78/92623/1.html

I think it's probably doing what Lex came up with (i.e. no trig needed).
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-12-2007 17:17
Yep, I found that thread, it's not the one I was thinking of :)

I'll try building a ring with that. I'm assuming I'll have to rez the blocks at the correct positions/rotations, and then use your Path Maker script to get them to line up their tapers/skews?

So... do you think it's possible to get that to do the surface of a spheroid? :)
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
03-12-2007 17:56
Well somewhere in there it's got a routine for figuring out how to get a box prim into the shape of an arbitrary trapesium (up to skew limits), defined by 4 vectors giving the positions of the 4 corners. I think for the spheroid however, you just need symmetric trapesia (i.e. with skew = 0).

I would probably be lazy and just use the parametric surface rezzer though ;p
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-12-2007 18:34
From: someone
I think for the spheroid however, you just need symmetric trapesia (i.e. with skew = 0).


You think so? That's how it'll work for a sphere, but a spheroid... think of a blimp, that's the shape I have in my head. So the faceted parts will look like the windsheld on a big commercial jet :)

Hmm... I guess it depends on how I divide it. I'd thought of two approaches - the one I'm using now is to have each piece subtend the same angle at the center. This would need different sized pieces, and would need skew as well I think. The other would be to use pieces that are equal fractions of the circumference. This would then have each piece subtend a different angle at the center, but maybe wouldn't need skewing.

From: someone
Well somewhere in there it's got a routine for figuring out how to get a box prim into the shape of an arbitrary trapesium (up to skew limits), defined by 4 vectors giving the positions of the 4 corners.


I looked at that again after reading Lex's response, and yes, that's exactly what my question asked. Should have spent some more time trying to understand your script the first time I looked at it :)


From: someone
I would probably be lazy and just use the parametric surface rezzer though ;p


I'll take a look at that too :)
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
03-12-2007 20:37
From: Ziggy Puff
You think so? That's how it'll work for a sphere, but a spheroid... think of a blimp, that's the shape I have in my head. So the faceted parts will look like the windsheld on a big commercial jet :)

Hmm... I guess it depends on how I divide it.
Oh right. I was thinking of something with dimensions (a,a,b), with the "equator" slice perpendicular to the b axis. But yes, if you turn it sideways, or choose all different axes then it needs skew too.
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-13-2007 00:03
OK, I got the 2D case to work pretty well. Modified Cadroe's script to place the prims around an ellipse, and rotate them so they're aligned along the tangent to the curve at that point (this seems to help Path Maker). Modified Path Maker into more of a Loop Maker, i.e. get prims 0 and N - 1 to recognize each other as left/right partners. Now I get a really nice ellipse :)

The 3D case goes completely crazy, since after the first layer, the next layer doesn't have all the prims in the same horizontal plane.

So I think the next thing I'll try is to take these "trapezoid coords ==> prim params" functions and try and feed them coordinates calculated for the surface of the spheroid.

From: someone
I was thinking of something with dimensions (a,a,b), with the "equator" slice perpendicular to the b axis.


Hmm. I'd been envisioning it the other way round too, like you described, but I guess I could try building it this way.