MySQL Forums
Forum List  »  Stored Procedures

MySql code to check if line intersects box is not working
Posted by: David Jones
Date: May 14, 2014 08:50PM

I am making a sql trigger/function to check if a line intersects a box. So far I have this

DELIMITER //
CREATE FUNCTION same_side_of_line(x1 float, y1 float, x2 float, y2 float, ax float, ay float, bx float, by_ float)
RETURNS bool
DETERMINISTIC
BEGIN
RETURN ((x1-x2)*(ay-y2)-(y1-y2)*(ax-x2)) * ((x1-x2)*(by_-y2)-(y1-y2)*(bx-x2)) >= 0;
END;//
DELIMITER ;

DELIMITER //
CREATE FUNCTION line_segment_intercept(ax1 float, ay1 float, ax2 float, ay2 float, bx1 float, by1 float, bx2 float, by2 float)
RETURNS bool
DETERMINISTIC
BEGIN
RETURN (NOT same_side_of_line(ax1,ay1,ax2,ay2,bx1,by1,bx2,by2)) AND (NOT same_side_of_line(bx1,by1,bx2,by2,ax1,ay1,ax2,ay2));
END;//
DELIMITER ;

DELIMITER //
CREATE FUNCTION check_box_collision(minX float, minY float, x1 float, y1 float, x2 float, y2 float)
RETURNS bool
DETERMINISTIC
BEGIN
SET @unit_size:=32;
SET @side1:=line_segment_intercept(x1,y1,x2,y2,minX,minY,minX+@unit_size,minY);
SET @side2:=line_segment_intercept(x1,y1,x2,y2,minX,minY,minX,minY+@unit_size);
SET @side3:=line_segment_intercept(x1,y1,x2,y2,minX,minY+@unit_size,minX+@unit_size,minY+@unit_size);
SET @side3:=line_segment_intercept(x1,y1,x2,y2,minX+@unit_size,minY+@unit_size,minX+@unit_size,minY);
RETURN @side1 + @side2 + @side3 + @side4 > 0;
END;//
DELIMITER ;

In the last function, (minX,minY) is the bottom left corner of the box, and the box is 32x32. (x1,y1) (x2,y2) is the line segment.

The code from the top two functions are from here:
https://www.allegro.cc/forums/thread/597053 post by `james_lohr`.

But this isn't working properly.

Does anyone see the problem?

Thanks

Options: ReplyQuote


Subject
Views
Written By
Posted
MySql code to check if line intersects box is not working
1910
May 14, 2014 08:50PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.