Log in

View Full Version : Simplifying



Main
02-03-2011, 05:08 AM
Can I simplify this further?
If da > 0 then
Begin
If T<180 then
Begin
If ((Fx < 0) and (Fy < Ny)) or ((Fx > 0) and (Fy < -1 * Ny)) then dA:=dA + 180;
End else
Begin
If ((Fx < 0) and (Fy > -1 * Ny)) or ((Fx > 0) and (Fy > Ny)) then dA:=dA + 180;
End;
End;

I know that

If a then b
else
c;
is possible.
but can

if e then c;

replace the b?


EDIT: I fixed the problem in the second embed, its still the same but I replaced "c end;" with "c;" which is correct but my question remains the same. I hope you guys can see what am on about.


EDIT 2: I realized my example can but I couldn't get it to work with what I have in the first embed. so it would be a if ..then within a if..then and within a if..then without begin and end if the final then ends up with a procedure like this;. Yeah this thing is a bit hard to explain.. I just want to see if i can save some space

Sex
02-03-2011, 05:24 AM
It can be shortened, one sec.

TomTuff
02-03-2011, 05:28 AM
if (da < 0) then
Exit;
if ((((Fx < 0) and (Fy < Ny)) or ((Fx > 0) and (Fy < -1 * Ny)) and (T < 180)) or (((Fx < 0) and (Fy > -1 * Ny)))) or (((Fx > 0) and (Fy > Ny)) and (T >= 180)) then
IncEx(dA, 180);

Ogre
02-03-2011, 05:40 AM
if (T < 180) then n := 1 else n := -1;
if dA > 0 then
begin
if (Fx < 0) and (n * Fy < Ny) then dA := dA + 180;
if (Fx > 0) and (-n * Fy > Ny) then dA := dA + 180;
end;

This is cleanest, imo. (I'm pretty sure it works :p)
You could also replace the n's with Sin(T)/Abs(Sin(T)) (degrees, not radians!) and remove the first if..then statement, but that would probably take longer and will give an error if T = 180 (If it never will be then you can also just replace the first if..then statement with n := Sin(T)/Abs(Sin(T)), but again that'd probably be slower)

Sex
02-03-2011, 05:45 AM
Yeah, I was gonna do something similar to Ogre's so nevermind :p.

Main
02-03-2011, 07:10 AM
thanks ogre, you actually fixed a mistake where I missed a begin and end in the first embed I posted.