GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2018-04-19 16:41:33

Tsa05
Member
Registered: 2016-11-16
Posts: 13

point_on_rectangle

Didn't see this after a bit of searching around, so I've gotta post it.

Expand///@description		point_on_rectangle(x, y, minX, minY, maxX, maxY)
///@arg	{real}	x		Coordinate of point outside rectangle
///@arg	{real}	y		Coordinate of point outside rectangle
///@arg	{real}	minX	Left X of box
///@arg	{real}	minY	Top Y of box
///@arg	{real}	maxX	Right X of box
///@arg	{real}	maxY	Bottom Y of box
/*
*	Note that this is not point_IN_rectangle
*
*	Returns the coordinates where a line between the
*	center of a rectangle and a given point intersects
*	the rectangle.
*/

var px = argument[0];
var py = argument[1];
var minX = argument[2];
var minY = argument[3];
var maxX = argument[4];
var maxY = argument[5];
var r = -1;
	
if (minX <= px && px <= maxX) && (minY <= py && py <= maxY){
	// Point should not be inside the rectangle!
	return r;
}
var midX = (minX + maxX) / 2;
var midY = (minY + maxY) / 2;
// if (midX - x == 0) -> m == ±Inf -> minYx/maxYx == x (because value / ±Inf = ±0)
var m = (midY - py) / (midX - px);

if (px <= midX) { // check "left" side
	var minXy = m * (minX - px) + py;
	if (minY <= minXy && minXy <= maxY){
		r[0] = minX; r[1] = minXy;
		return r;
	}
}

if (px >= midX) { // check "right" side
	var maxXy = m * (maxX - px) + py;
	if (minY <= maxXy && maxXy <= maxY){
		r[0] = maxX; r[1] = maxXy;
		return r;
	}
}

if (py <= midY) { // check "top" side
	var minYx = (minY - py) / m + px;
	if (minX <= minYx && minYx <= maxX){
		r[0] = minYx; r[1] = minY;
		return r;
	}
}

if (py >= midY) { // check "bottom" side
	var maxYx = (maxY - py) / m + px;
	if (minX <= maxYx && maxYx <= maxX){
		r[0] = maxYx; r[1] = maxY;
		return r;
	}
}

// Somehow...
show_debug_message("Something didn't work in point_on_rectangle");
return r;

This is basically like... the "Smash Brothers Code"... Something is outside of a defined rectangular area, and you want to show where it has gone with a visual indicator that is on or within the rectangle.
Icon drawn on a rectangle boundary

Offline

Board footer

Powered by FluxBB