简体   繁体   English

如何在多边形上获得Y位置

[英]How get an Y position on a Polygon

I have a polygon in a 2d game in Java. 我在Java 2D游戏中有一个多边形。 How can I get an Y-position on the polygon, from a given X-position on the polygon. 如何从多边形上的给定X位置获取多边形上的Y位置。

See picture: 看图片:

游戏,多边形

I want to place the player on the cross, but I dont know how to get out a Y position thats on the polygon. 我想将玩家放在十字架上,但是我不知道如何找出多边形上的Y位置。

Here's some more info, the polygon is of type, http://docs.oracle.com/javase/7/docs/api/java/awt/Polygon.html . 这是更多信息,多边形的类型为http://docs.oracle.com/javase/7/docs/api/java/awt/Polygon.html

Here's how it's created: 它是如何创建的:

 private void paintGround(Graphics2D g2d){
    int width =  gameBoard.getWidth(); //1000
    int height = gameBoard.getHeight(); //750
    int xCoords[] = {0,0,width/2,width,width};
    int yCoords[] = {height,height/2,height/2-100,height/2,height};
    g2d.setColor(Color.white);
    Polygon polygon = new Polygon(xCoords, yCoords,5);
    g2d.fillPolygon(polygon);
    }

Thanks for your time and sorry for the stupid question :) 谢谢您的时间,对不起这个愚蠢的问题:)

Here's the git, if anyone is interested: https://github.com/davli921/Smrow.git 如果有人感兴趣的话,这里是git: https : //github.com/davli921/Smrow.git

Here's a more general solution for getting the height at a particular point between two vertices: 这是获取两个顶点之间特定点的高度的更通用的解决方案:

  • Define the "mountains" as an array, with the indices representing the X values and the actual contents representing the Y (height) values. 将“山”定义为一个数组,其中的索引表示X值,实际内容表示Y (高度)值。
  • Use this information to calculate the Y position for the "player" based on their current X value. 使用此信息可基于“玩家”的当前X值计算其Y位置。
  • Draw the results. 绘制结果。

The example below has been implemented in Javascript so that anyone can easily try it out in their browser, but the principle is the same no matter what language you are using. 下面的示例已使用Javascript实现,因此任何人都可以在其浏览器中轻松尝试它,但是原理是相同的,无论您使用哪种语言。 First we need to define some basic HTML, so that we have a canvas to draw to: 首先,我们需要定义一些基本的HTML,以便绘制一个画布:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Example</title>
  </head>
  <body>
    <canvas id="game_canvas" width="400" height="300">You must enable Javascript</canvas>
    <script src="game.js"></script>
  </body>
</html>

Now, here's the important part - the Javascript: 现在,这是重要的部分-Javascript:

"use strict";

// This series of points represents the "height" values of the landscape.
var points = [10, 50, 150, 20, 100, 10];

// Let's pick a random X position...
var x = Math.random() * (points.length-1);

// We can get the height at a given point by interpolating the vertices:
var v1 = points[Math.floor(x)];
var v2 = points[Math.ceil(x)];
var y = v1 + ((v2-v1) * (x % 1.0));

// Now let's draw the "game" state...
var canvas = document.getElementById("game_canvas");
var ctx = canvas.getContext("2d");
var x_scale = canvas.width / points.length;

// This is the "world"...
ctx.moveTo(0, points[0]);
for (var p = 1; p < points.length; ++p) { ctx.lineTo(x_scale * p, points[p]); }
ctx.stroke();

// ...and this is the "player".
ctx.fillStyle = "red";  
ctx.fillRect((x * x_scale)-4, y-4, 8, 8);

Save these files to a local folder (ensuring that the JS file is saved as game.js ) and open the HTML file in your browser: 将这些文件保存到本地文件夹(确保JS文件另存为game.js ),然后在浏览器中打开HTML文件:

图片

Press F5 to refresh the page (and thus pick a new random X position). F5刷新页面(从而选择一个新的随机X位置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM