我有一個數學問題。我在做一個遊戲,用戶是一個12歲的孩子。孩子的目標是計算繪製形狀的面積。在簡單和中等模式下,形狀被給出並且被硬編碼,因此它們不是硬核。在硬模式中,5個座標是隨機生成的,這裏就是問題出現的地方。我需要製作一個可以由12歲孩子計算的區域。隨機座標會出現各種困難的情況,例如交叉點,或連接其他兩點的線上的奇點等等。有什麼方法可以計算和避免這些問題嗎?數學計算:比較座標以使合理的形狀
這裏是我的代碼,這使得隨機點+吸引它在應用點網格:
private void gameHard()
{
//distance between points is 65 pixels, the numbers that are generated are 1-8
x1=(genRandomInt())*65;
x2=(genRandomInt())*65;
x3=(genRandomInt())*65;
x4=(genRandomInt())*65;
x5=(genRandomInt())*65;
y1=(genRandomInt())*65;
y2=(genRandomInt())*65;
y3=(genRandomInt())*65;
y4=(genRandomInt())*65;
y5=(genRandomInt())*65;
compareRCoordinates();
areaImage = new JPanel()
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
g2.fillRect(0,0,780,650);
g2.setColor(Color.BLACK);
int xnum = 65, ynum = 65;
for(ynum=65;ynum<650;ynum=ynum+65)
{
int x=0, y=0;
for(xnum = 65;xnum<780;xnum=xnum+65)
{
x = xnum-9;
y = ynum-9;
g2.fillOval(x,y,18,18);
}
xnum=xnum+65;
}
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(6));
g2.drawLine(x1,y1,x2,y2);
g2.drawLine(x2,y2,x3,y3);
g2.drawLine(x3,y3,x4,y4);
g2.drawLine(x4,y4,x5,y5);
g2.drawLine(x5,y5,x1,y1);
}
};
areaImage.setBounds(20,20,780,650);
areaImage.setBorder(BorderFactory.createLineBorder(Color.black));
this.add(areaImage);
roundsPlayed++;
}
你打算包含哪些類型的形狀?它總是長方形嗎?平行四邊形,梯形,菱形?三角形?界?還是你想要這個硬模式有五個座標的凸或凹形狀? – mbomb007
查看http://stackoverflow.com/questions/19238608/randomly-generating-a-shape使用Java2D的簡單隨機形狀的例子 – copeg
@ mbomb007一般任何形狀的5分;) –