1

You will see'm new to this forum and I would like to ask for your help with a code that does not get it to work at all.

Anyway, I'm trying to set a code to extend the measures of a line until it touches the edges of a rectangle containing the line.

I did not know how to code so look for information on the Internet and found a code in pyton supposedly to what I wanted and I translated to javascript.

The code is in the following link:

If you want to see exactly what I want to do the link is an image that explains it.

enter image description here

https://stackoverflow.com/questions/7237004/extending-a-line-segment-to-fit-into-a-bounding-box

And it is as follows:

function extendline(xmin, ymin, xmax, ymax, x1, y1, x2, y2) {
    //alert("function extendline")
    var points = [];

    if (y1 == y2) {
        //return (xmin, y1, xmax, y1)
        points[0] = xmin;
        points[1] = y1;
        points[2] = xmax;
        points[3] = y1;
        //alert("y1 == y2" + "   |   " + "points: " + points)
        return points
    }
    if (x1 == x2) {
        //return (x1, ymin, x1, ymax)
        points[0] = x1;
        points[1] = ymin;
        points[2] = x1;
        points[3] = ymax;
        //alert("x1 == x2" + "   |   " + "points: " + points)
        return points
    }

    y_for_xmin = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
    y_for_xmax = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)

    //alert("y_for_xmin: " + y_for_xmin)
    //alert("y_for_xmax: " + y_for_xmax)

    x_for_ymin = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
    x_for_ymax = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)

    //alert("x_for_ymin: " + x_for_ymin)
    //alert("x_for_ymax: " + x_for_ymax)

     if(ymin <= y_for_xmin && y_for_xmin <= ymax) {
        //alert("ymin <= y_for_xmin <= ymax")
        if (xmin <= x_for_ymax && x_for_ymax <= xmax) {
            //return (xmin, y_for_xmin, x_for_ymax, ymax)
            points[0] = xmin;
            points[1] = y_for_xmin;
            points[2] = x_for_ymax;
            points[3] = ymax;
            //alert("xmin <= x_for_ymax <= xmax" + "   |   " + "points: " + points)
            return points
        }
        if (xmin <= x_for_ymin && x_for_ymin <= xmax) {
            //return (xmin, y_for_xmin, x_for_ymin, ymin)
            points[0] = xmin;
            points[1] = y_for_xmin;
            points[2] = x_for_ymin;
            points[3] = ymin;
            //alert("xmin <= x_for_ymin <= xmax" + "   |   " + "points: " + points)
            return points
        }
    }
    if (ymin <= y_for_xmax && y_for_xmax <= ymax) {
        if (xmin <= x_for_ymin && x_for_ymin<= xmax) {
            //return (x_for_ymin, ymin, xmax, y_for_xmax)
            points[0] = x_for_ymin;
            points[1] = ymin;
            points[2] = xmax;
            points[3] = y_for_xmax;
            //alert("ymin <= y_for_xmax <= ymax" + "   |   " + "points: " + points)
            return points
        }
        if (xmin <= x_for_ymax && x_for_ymax <= xmax) {
            //return (x_for_ymax, ymax, xmax, y_for_xmax)
            points[0] = x_for_ymax;
            points[1] = ymax;
            points[2] = xmax;
            points[3] = y_for_xmax;
            //alert("xmin <= x_for_ymax <= xmax" + "   |   " + "points: " + points)
            return points
        }
    }
}

The code needs rectangle coordinates (x and y minimum and maximum) and the coordinates of the points that form the line (x and y point 1 and point 2)

The code returns an array of new coordinates of the points.

Now I tell you the problem.

If the line is horizontal or vertical everything works properly, but if the line is diagonal sometimes it works and sometimes does not return me values (thus I conclude that there is any condition that is not fulfilled and does not return any security)

The line randomly gender (always within the rectangle) so that the point 1 may be both the right of the line or left, up or down) I mention this because if affects not (I think not)

And that's all the information I can give you because they do not really understand the operation of the code.

Would greatly appreciate your help please. Actually I'm not a programmer (amateur only) and find it hard to put together these codes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.