Jump to content
LordJashin

OSI, New MATH functions from MSSL!

Recommended Posts

Also Split Divi/Array.scar into Point.scar, Color.scar, Array.scar, and String.scar

 

https://github.com/OSI1/OSI1/blob/master/Divi/Math.scar

 

Janilabo's library may not be fully documented, but it is awesome nonetheless, and I bet he put a crap load of time into it. So hopefully putting a lot of it in OSI, will beef it up, and become very useful.

 

I love these Odd, Even functions! New list here and the (+) are the new ones:

 

[sCAR]

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Official SCAR Include

Math Routines

--------------------------------------------------------------------------------

+ * procedure IncE(var x: Extended; N: Extended);

+ * procedure DecE(var x: Extended; N: Extended);

+ * function Odd(x: Integer): Boolean;

+ * function Even(x: Integer): Boolean;

+ * function Difference(value1, value2: Integer): Integer;

+ * function DifferenceE(value1, value2: Extended): Extended;

+ * function ValueToPercent(position, source: Extended): Extended;

+ * function PercentToValue(percent, source: Extended): Extended;

+ * function ValueToFraction(position, source: Extended): Extended;

+ * function FractionToValue(fraction, source: Extended): Extended;

+ * function InRangeE(value, minimum, maximum: Extended): Boolean;

+ * function RandomEx(range, amount: Integer; duplicates: Boolean): TIntArray;

* function RR(Min, Max: Integer): Integer;

+ * function RandomRangeEx(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntArray;

* function RRectanglePoint(X1, Y1, X2, Y2: Integer): TPoint;

* function RCirclePoint(CX, CY, Radius: Integer): TPoint;

+ * function GetAngle(x, y, x2, y2: Integer): Extended;

+ * function GetAngle2(pt1, pt2: TPoint): Extended;

+ * function GetAngleBetweenPoints(pt1, pt2: TPoint): Extended;

* function AngleBetweenPointsDegress(P1, P2: TPoint): Extended;

* function DistanceEx(X1, Y1, X2, Y2: Extended): Extended;

+ * function DistanceEx2(pt1, pt2: TPoint): Extended;

* procedure ConstrainBox(var X1, Y1, X2, Y2: Integer; CX1, CY1, CX2, CY2: Integer);

* procedure ConstrainBoxes(var TBA: TBoxArray; CX1, CY1, CX2, CY2: Integer);

* function GenerateSpiral(Center: TPoint; AngStep, DistStep: Extended; X1, Y1, X2, Y2: Integer): TPointArray;

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

 

[/sCAR]

 

Its been a while since I've taken another look at Geometry methods. It was though, by far my favorite Math Class. So what is the GetAngle and GetAngleBetweenPoints functions, what does it get exactly?

Edited by LordJashin
Link to comment
Share on other sites

You should replace GetAngle with GetAngle2, as both of them aren't needed really.. Just taking space (because they both do the same thing anyways).

Also, I recommend deleting that GetAngleBetweenPoints too, because its rather buggy (I deleted it from latest MSSL aswell), and it should do just about the same thing as GetAngle.. So thats wasted space there aswell.

 

But yeah, GetAngle returns the compass angle of 2 points.

 

GetAngle example:

function GetAngle(pt1, pt2: TPoint): Extended;
begin
 Result := ((ArcTan2((pt2.Y - pt1.Y), (pt2.X - pt1.X)) * (180.0 / Pi)) + 90.0);
 if (Result < 0.0) then
   Result := (Result + 360.0);
end;

var
 i: Integer;
 c_pt: TPoint;
 TPA: TPointArray;

begin
 ClearDebug;
 c_pt := Point(182, 182);
 TPA := [Point(0, 0), Point(182, 0), Point(363, 0), Point(363, 182), Point(363, 363), Point(182, 363), Point(0, 363), Point(0, 182)];
 for i := 0 to High(TPA) do
   WriteLn(FloatToStr(GetAngle(c_pt, TPA[i])) + ' - (' + IntToStr(c_pt.X) + ',' + IntToStr(c_pt.Y) + ') => (' + IntToStr(TPA[i].X) + ',' + IntToStr(TPA[i].Y) + ')');       
 SetLength(TPA, 0);
end.

 

I also recommend doing the same thing with DistanceEx and DistanceEx2.. Replace DistanceEx with DistanceEx2. For saving some space, 1 function is enough really. :)

 

BTW, update your MSSL.. I have improved version of RandomRangeEx in it, the version you included there gets really slow quickly with bigger arrays..

Link to comment
Share on other sites

Odd and Even sounds nice, I'm going to put those in SCAR :D However, this method will be way faster:

[scar]function Odd(x: Integer): Boolean;

begin

Result := x and 1 = 1;

end;

 

function Even(x: Integer): Boolean;

begin

Result := x and 1 = 0;

end;[/scar]

 

Yay! I've been wanting those in SCAR for like forever xD! woot!

I need to go through MSSL again, cuz it seems Jani keeps changing stuff around. I got the repo now lols.

Link to comment
Share on other sites

Odd and Even sounds nice, I'm going to put those in SCAR :D However, this method will be way faster:

[scar]function Odd(x: Integer): Boolean;

begin

Result := x and 1 = 1;

end;

 

function Even(x: Integer): Boolean;

begin

Result := x and 1 = 0;

end;[/scar]

 

1 = 0?

 

As far as I know, this is highly illegal in a lot of languages. What's the logic that doesn't make it illegal in SCAR?

Link to comment
Share on other sites

1 = 0?

 

As far as I know, this is highly illegal in a lot of languages. What's the logic that doesn't make it illegal in SCAR?

 

The logic is the same as in math and any other programming language... The order of operations. It doesn't say "x and (1 = 0)", it says "(x and 1) = 0". It is a bitwise operation. "and 1" select the last bit in the number, which is the same result you get from "mod 2", but it's a far less expensive operation.

Link to comment
Share on other sites

The logic is the same as in math and any other programming language... The order of operations. It doesn't say "x and (1 = 0)", it says "(x and 1) = 0". It is a bitwise operation. "and 1" select the last bit in the number, which is the same result you get from "mod 2", but it's a far less expensive operation.

 

Huh, I thought it was just saying. If x and 1 = false its odd. If x and 1 = true its even. Yeah that's gotta be what it means.

 

So since 0 = false, and 1 = true. We get these: x and 1 = 1

x and 1 = 0

 

the and 1, if Freddy says, its like using mod. Then and 1 divides it and gives remainder?

Edited by LordJashin
Link to comment
Share on other sites

It is confusing until you realize and does double duty.

 

There is the logical and you are used to that equates to True/False. Thinking of this in those terms is misleading and more than a little confusing.

 

Then there is the bitwise and (proper terminology???) that compares the bits (the # in binary form) and comes up with another number. If a bit is set in the first number and the bit is set in the second number then it is set in the result.

 

His example above is counting on the fact that when expressed as binary the rightmost (least significant) bit is always set for an odd number. Always.

 

Let me give you an example using the number 7 (I'll use a byte value to keep it simple)

 

00000111 - 7 expressed as binary (128*0 + 64*0 + 32*0 + 16*0 + 8*0 + 4*1 + 2*1 + 1*1)
00000001 - 1 expressed as binary (128*0 + 64*0 + 32*0 + 16*0 + 8*0 + 4*0 + 2*0 + 1*1)
00000001 - Result = 1 so number is Odd

 

I suck at explaining things. Hope this helps a little.

 

Edit: Technically Freddy is correct: Any odd number = True and any even number = False but this is not guaranteed across compilers so stick to 1=True, 0=False if doing funny stuff like treating an integer as a boolean.

 

Edit2: FINALLY I sneak one in ahead of Freddy! <happy dance>

Edited by Bixby Sayz
Link to comment
Share on other sites

Huh, I thought it was just saying. If x and 1 = false its odd. If x and 1 = true its even. Yeah that's gotta be what it means.

 

So since 0 = false, and 1 = true. We get these: x and 1 = 1

x and 1 = 0

 

the and 1, if Freddy says, its like using mod. Then and 1 divides it and gives remainder?

 

You're looking at it the wrong way... 1 or 0 do not represent boolean values here. Neither does "and 1" perform a division, it's a bitmask. And has 2 functions, it can function as a boolean operator or a bitwise operator, in this case it does the latter.

 

If you have the value 155, this is an uneven number represented by the binary number 10011011. The "and 1" select the last bit in that number (10011011). This bit is 1 because it's uneven. This is also, not by coincidence, the remainder of the integer division by 2, but checking a bitmask is NOT the same as performing a division. Dividing is a very expensive operation for a CPU to perform, where as this is just getting the last bit of the number, which is all we need as we want to be "dividing by 2".

 

So for example, the value 50 is 110010. The last bit is 0, so this value is even.

 

 

One final way of looking at it: the bitwise operation "and 1" select the last bit in the number because a bitmask consisting out of 1's and 0's will, when applied with the and operator, return the original number with all of the original bits where the mask is 1 and 0's where the mask is 0. So if you have a value of lets say 1101 and you apply the mask 1010, it will return 1000 because the first 1 is kept as the mask has a 1, the second 1 is set to 0 because the mask has a 0, then the 0 is kept because the mask has 1 (True and False = False) and finally the last 1 is set to 0 as the mask has a 0 for the last number.

Link to comment
Share on other sites

Thanks guys...

I appreciate the explaining, now I want to go learn bitwise operations again! If only I had shadow to teach me it again lols. Just need to remember how everything is shuffled around, and then operations and I'll be fine. Just need to make an example/testing file again!

 

At least I remember what they are: shl, shr, and, or, xor

 

Can you perform bitwise operations on an Extended number?

 

In other news: I added more string functions from MSSL with some minor tweaking - here.

The String Testing file here.

 

The dang Box functions test file took real long - https://github.com/OSI1/OSI1/blob/master/Divi/Testing/BoxFunctions.scar

Edited by LordJashin
Link to comment
Share on other sites

Can you perform bitwise operations on an Extended number?

 

There's nothing preventing you from doing that, but afaik there isn't much of a use for it due to the way floating point numbers are structured. The only use I can think of is to take the number apart and individual expose the components. But I think there's little or any practical use for bitwise operations with floating point numbers.

  • Confused 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...