forsyth: (DotDotDot)
Forsyth ([personal profile] forsyth) wrote2009-07-17 06:36 pm
Entry tags:

Math/Programming Time! (with questions!)

Okay, here's a mat puzzle for you, folks. I'm trying to create a 3d angled sloped wedge. With for loops, on a 3d X,Y,H grid.

The base is a triangle, I know it's a right triangle. I know the length and the height of it. I know the angle at one tip. The wedge is also a right triangle, (well, until I cut off the top at a specified height, but that's just a matter of a simple if H(i,j) > hc, H(i,j)=hc statement) and I know the angle of that slope too.

I want to find the X,Y coordinates of the hypotenuse every step along the length. Well, the X coordinate, since I'm counting along the Y axis. Once I have that, I should be able to increment from that, to the back of the triangle, and calculate the height at each step along there, which should be easy. Famous last words. There's a picture in the cut below



So here's how my thinking's gone. First, an outer For loop to increment along the length. Simple For j=1:Y style loop (yes, I'm using Matlab. And technically, it's y1:mid, since the wedge is supposed to be sloped on both sides, but if one works, then the other should too). So far so good.

Next, to find where the hypotenuse is, to count along the X axis. Since tangent is opposite/adjacent, and my X axis is adjacent to the angle I know, I'm trying to find Z by doing 1/[tan(theta)*j (the opposite)] That should get me 1/1/adjacent, which should come out to adjacent. Somehow, I keep getting divide by zero errors here.

I should just copy/paste the code, huh?

for j=j1:mid;
z=floor(1/(tan(theta)*j)+1; % The +1 is to avoid starting by accessing a 0 point in the matrix.
for i=z:xc1; % z to the back wall.
H(j,z)=tan(2*pi/3)*j-H(j,z); % SHOULD calculate H, then add it to the original H at the base of the slope, to keep the slope constant
if H(j,z) > hc
H(j,z) = hc % Just a simple check to see if it's reached the max height, and cut it there
end % end of the if
end % end of the i loop
end % end of the j loop

Obviously, it's not working. The H array is my initial grid and the height at those points. Obviously, it's not working, which is why I'm posting about it. I think it's the z definition that's not working, but I can't figure out why. It should be, as far as I can tell.


So yeah, any help anybody can offer would be great.

[identity profile] colinmo.livejournal.com 2009-07-18 02:14 am (UTC)(link)
As a thought:

You have a opposite side of Y and an adjacent of X. Y goes from 0 to Y, meaning you have Y sections on opposite. So in the move from 0 to Y along the Y axis, you're moving from 0 to X along the X axis in X/Y increments. That way when you've moved to Y (1 * Y), you've moved Y * Y/X spaces along X, meaning you end up on X. How's that figure out?

[identity profile] forsythferret.livejournal.com 2009-07-18 03:06 am (UTC)(link)
The step distances are already set by the external grid, which is one of the things that makes this difficult. I'm trying to model a surfing reef on an ocean floor, right now it's a basic theoretical plane, but the program should be general enough to deal with any layout.

[identity profile] alun-clewe.livejournal.com 2009-07-19 08:20 am (UTC)(link)
>Next, to find where the hypotenuse is, to count along the X axis. Since tangent is opposite/adjacent, and my X axis is adjacent to the angle I know, I'm trying to find Z by doing 1/[tan(theta)*j (the opposite)] That should get me 1/1/adjacent, which should come out to adjacent.

No, that won't give you 1/1/adjacent. What that gives you is 1/[(opposite/adjacent)*opposite]... that doesn't come out to 1/1/adjacent; it comes out to adjacent/opposite^2, which isn't what you want. What you want is j/tan(theta).

That doesn't explain the divide by zero error, though. Not sure what's going on with that... (Unless j1 is zero?)

[identity profile] forsythferret.livejournal.com 2009-07-21 11:21 pm (UTC)(link)
Hmm, I think you're right. It's always the stupid tiny things that cause the trip-ups. I'd gotten in to work by changing it to sin, but I knew the angles were wrong. That does seem to have fixed it. Thanks!