How to convert screen coordinates to Cartesian coordinates?

4

In Python: How to convert screen coordinates to Cartesian coordinates, where there may be positive and negative points and the center of the screen is (0,0)?

    
asked by anonymous 18.02.2014 / 17:34

1 answer

6

This is a problem of coordinate system conversion and considering that your programming language has (0, 0) the top left corner of the screen we have (in JavaScript - in Python it's analog):

<div style="width: 600px; height: 300px; border: 1px #000 solid; padding: 0px; margin: 0px;">


var larguraTela = 600;
var alturaTela = 300;
var getXY = function(x, y) {
    var novoX =  x - (larguraTela / 2);
    var novoY = (y - (alturaTela / 2)) * -1;
    return [ novoX, novoY ];
}

document.addEventListener('click', function(evt) {
    console.log(evt.x + ", " + evt.y);
    console.log(getXY(evt.x, evt.y));
}, false);

I did in javaScript / HTML to test in the browser giving clicks on the rectangle and looking at the browser console, but what matters is the getXY () function that expects as a parameter the original X and Y coordinates and returns an array of two elements : x and y converted.

Notice that you need to multiply by (-1) in Y because of reverse direction from the traditional Cartesian system .

    
18.02.2014 / 18:41