GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2013-07-13 18:13:39

Marchal_Mig12
Member
Registered: 2009-05-21
Posts: 72

Point intersections between a line and a circle (solved)

Hi,

Edit : nevermind, it works. Feel free to try to optimise it and post it on gmlscripts.

var x1 = argument0;
var y1 = argument1;
var x2 = argument2;
var y2 = argument3;
var circleX = argument4;
var circleY = argument5;
var circleR = argument6

var dx = x2 - x1;
var dy = y2 - y1;

var denominator = sqr(dx) + sqr(dy);

if ( denominator > 0 ) {
    var position = ( ( circleX - x1 ) * dx +( circleY - y1 ) * dy ) / denominator;

    var px = x1 + position * dx;
    var py = y1 + position * dy;

    var m = point_distance( px, py, circleX, circleY ) / circleR;

    if ( m <= 1 ){
        var offset = circleR * sin( arccos( m ) );
        var mag = sqrt( denominator );
        dx /= mag;
        dy /= mag;
        
        //Intersection points
        ix1 = px + offset * dx;
        iy1 = py + offset * dy;
        ix2 = px - offset * dx;
        iy2 = py - offset * dy;
        
        return true;
    }else{
        return false;
    }
}

Thanks

Last edited by Marchal_Mig12 (2013-07-13 18:18:00)

Offline

#2 2013-07-13 19:04:54

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

Re: Point intersections between a line and a circle (solved)

Heh, OK. I'll check it out. smile


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB