GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2015-09-17 10:59:01

Mortalo
Member
Registered: 2015-09-17
Posts: 4

line_rotate()

(Most of my code is quite personal and direct and it's hard to make a single script like that of it. But this one is different.)
I'm currently working with primitives and vertexes and I needed a way to rotate the triagle. I didn't noticed anything like that in GM or here, so I went and did it myself. It takes 2 points, 1st is root and 2nd is the one we want position to change. This script has no restrictions to points position, I hope that you'll find it useful.

Expand/// line_rotate(x1,y1,x2,y2,angle);
//
//  Returns new x2 and y2 values (as an array, where [0] is new x2 value
//	and [1] is new y2 value), rotated by angle.
//
//
//      x1      root x
//      y2      root y
//      x2      end of line/rotated point x value
//		y2		end of line/rotated point y value
//		angle	angle in radians 	
//
//
/// GMLscripts.com/license

var _xo, _yo, _x, _y, _angle;
var _a, _b, _c, _a2, _b2, _beta, _beta2, _arr;

_xo = argument0;
_yo = argument1;
_x = argument2;
_y = argument3;
_angle = argument4;

_a = _x - _xo;
_b = _yo - _y;
_c = sqrt(_a*_a + _b*_b);

//We're dividing by _a, so we need to check if _a isn't zero
if(_a == 0)
    if(_b > 0)
        _beta2 = pi/2;
    else
        if(_b < 0)
            _beta2 = -pi/2;
        else
		{
			_arr[1] = _y;
			_arr[0] = _x;
			return _arr;
		}

_beta = arctan2(_b,_a);
_beta2 = _angle + _beta;

_b2 = _c * sin(_beta2);
_a2 = _c * cos(_beta2);

_arr[1] = _yo - _b2;
_arr[0] = _xo + _a2;

return _arr;

PS. English is not my native but I hope that I expressed all that I should in the way that you'll understand smile
PS2. I struggled with name of the script.

Offline

#2 2015-09-17 16:06:03

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: line_rotate()

Interesting idea for a script.

GM:Studio has some useful functions that can simplify this.

Expand/// line_rotate(x1,y1,x2,y2,angle);
//
//  Returns an array of coordinates (x2,y2) rotated
//  about the origin point (x1,y1) by a given angle.
//
//      x1,y1       origin point, real
//      x2,y2       end point, real
//      angle       angle, radians
//
/// GMLscripts.com/license
{
    var x1, y1, x2, y2, angle, arr;
    x1 = argument0;
    y1 = argument1;
    x2 = argument2;
    y2 = argument3;
    angle = argument4;
    
    var dist = point_distance(x1, y1, x2, y2);
    var dir = point_direction(x1, y1, x2, y2);
    dir += radtodeg(angle);
    
    arr[1] = y1 + lengthdir_y(dist, dir);
    arr[0] = x1 + lengthdir_x(dist, dir);
    
    return arr;
}

Abusing forum power since 1986.

Offline

#3 2015-09-18 10:14:15

Mortalo
Member
Registered: 2015-09-17
Posts: 4

Re: line_rotate()

I'm curious why I didn't seen point_direction() in Manual...

Question: Wouldn't your version be slower? With all this script calls?

Offline

#4 2015-09-18 18:47:34

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: line_rotate()

Regarding speed, it depends on the target platform. In most cases they are probably very close.

I ran some benchmarks to test their relative speeds. The benchmarks ran 1000 iterations of 10,000 sets of randomly generated preselected arguments. Tests were performed on a Xeon E3-1231 v3 @ 3.4GHz.

I found:

Windows (Interpreter) runs the second version 9% faster at 0.527 μs per call.

Windows (YYC) runs the first version 14% faster at 0.201 μs per call.

HTML5 runs the first version 89% faster at 0.242 μs per call.

While the HTML5 differences are fairly drastic, the overall cost of either script is minuscule.

EDIT:

I no longer have faith in those raw execution times. Later benchmarks were much different (though still very fast). The relative performance of the scripts remains consistent.

Last edited by xot (2015-09-19 21:18:05)


Abusing forum power since 1986.

Offline

#5 2015-09-19 12:04:17

Mortalo
Member
Registered: 2015-09-17
Posts: 4

Re: line_rotate()

Ha, interesting. Worst part is that the 2nd one shows that I shouldn't spend so much time on making that script laugh I wanted to do it more complicated way, but it ended when I realized that it is impossible, so had to stick to the dump version.
Cool.

Offline

#6 2015-09-19 21:15:13

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: line_rotate()

My main intention with the second version was to show an intuitive way to do it.

I took another pass at this function and the result is somewhat faster overall.

Expand/// line_rotate(x1,y1,x2,y2,angle);
//
//  Returns an array of coordinates (x2,y2) rotated
//  about the origin point (x1,y1) by a given angle.
//
//      x1,y1       origin point, real
//      x2,y2       end point, real
//      angle       angle, radians
//
/// GMLscripts.com/license
{
    var x3 = argument2 - argument0;
    var y3 = argument3 - argument1;
    var ca = cos(argument4);
    var sa = sin(argument4);
    
    var arr;
    arr[1] = argument1 - x3 * sa + y3 * ca;
    arr[0] = argument0 + x3 * ca + y3 * sa;

    return arr;
}

With the interpreter, it's about 30% faster than the original.
With the compiler, it's about 3% faster than the original.
With HTML5, it's 79% faster than the original.

With the interpreter, most of that speed gain was from using the arguments directly rather than assigning them to variables. Those changes make almost no difference with the compiler and HTML5.

I think I may call this point_rotate() when it is added to the site.


Abusing forum power since 1986.

Offline

#7 2015-09-20 10:54:27

Mortalo
Member
Registered: 2015-09-17
Posts: 4

Re: line_rotate()

Point_rotate() is good, I have it named similiarly (scr_PointRotateByPoint), point_rotate sounds way better.
I never liked trigonometry, but tbh in school we didn't used it in so fun things.

Last edited by Mortalo (2015-09-20 10:54:43)

Offline

Board footer

Powered by FluxBB