import Felgo 3.0
import QtQuick 2.0
GameWindow {
id: gameWindow
activeScene: scene
screenWidth: 960
screenHeight: 640
Scene {
id: scene
// the "logical size" - the scene content is auto-scaled to match the GameWindow size
width: 480*2
height: 320*2
PolygonItem {
id: pol
fill: true
color:"grey"
vertices: [
Qt.point(20, 20),
Qt.point(200,40),
Qt.point(80, 20),
Qt.point(600, 100),
Qt.point(0, 100)
]
MouseArea {
anchors.fill: parent
// when the rectangle that fits the whole scene is pressed, change the background color and the text
onPressed: {
var mx = mouse.x
var my = mouse.y
var ins = inside( Qt.point(mx,my), pol.vertices );
console.debug("pressed position:", mouse.x, mouse.y, ins)
}
}
}
}
function inside(point, vs) {
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
var x = point.x, y = point.y;
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i].x, yi = vs[i].y;
var xj = vs[j].x, yj = vs[j].y;
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
}