Estimating Area Between Shapes - java

In my class, we are given the task as follows:
For this assignment you will attempt to make a program for computing the overlapping area of a set of shapes, implementing Monte Carlo integration, as discussed in class.
Make AreaEstimator.java, a program that estimates the area of overlap of an arbitrary number of circles and triangles using randomized estimation. The program's arguments will be the number of randomly-generated points to be used in this trial, followed by the coordinates of the points that define the shapes. For example,
java AreaEstimator 1000000 circle 2.0 2.0 1.0 triangle 1.0 1.0 2.5 3.0 2.0 -3.0 circle 2.5 1.0 3.0
would generate one million random points to estimate the overlap of a triangle whose vertices are ( 1.0, 1.0 ), ( 2.5, 3.0 ), ( 2.0, -3.0 ), with two circles whose centers are at ( 2.0, 2.0 ) and ( 2.5, 1.0 ), and whose radii are 1.0 and 3.0, respectively.
Here is my code for the Circle class:
public class Circle {
private double xcenter;
private double ycenter;
private double radius;
private double xcmax;
private double xcmin;
private double ycmax;
private double ycmin;
public Circle ( double xcenter, double ycenter, double radius){
if(radius <= 0){
throw new IllegalArgumentException();
}
this.xcenter = xcenter;
this.ycenter = ycenter;
this.radius = radius;
}
public void maxAndMinCircle (){ //find the minimun and maximum for the plane according to this circle
xcmax = (this.xcenter + this.radius);
ycmax = (this.ycenter + this.radius);
xcmin = (this.xcenter - this.radius);
ycmin = (this.ycenter - this.radius);
}
public double getXCMax (){
return xcmax;
}
public double getYCMax () {
return ycmax;
}
public double getXCMin() {
return xcmin;
}
public double getYCMin(){
return ycmin;
}
public boolean outsideCircle(double randX, double randY){ // find if the random point passed thru is in this circle
double distance = Math.sqrt((randX-this.xcenter)*(randX-this.xcenter) + (randY-this.ycenter) * (randY - this.ycenter));
return distance >= radius;
}}
The Triangle Class:
public class Triangle {
private double cornerx1;
private double cornery1;
private double cornerx2;
private double cornery2;
private double cornerx3;
private double cornery3;
private double xtmax;
private double xtmin;
private double ytmax;
private double ytmin;
private Double[] corners;
public Triangle (double cornerx1, double cornery1, double cornerx2, double cornery2, double cornerx3, double cornery3){
corners = new Double [6];
corners[0] = cornerx1;
corners[1] = cornery1;
corners[2] = cornerx2;
corners[3] = cornery2;
corners[4] = cornerx3;
corners[5] = cornery3;
}
public void maxAndMinTriangle (){ // find the minimum and maximum of the plane according to this triangle
xtmax = corners[0];
for(int i=1;i < corners.length;i += 2){
if(corners[i] > xtmax){
xtmax = corners[i];
}
}
xtmin = corners[0];
for(int i=1;i < corners.length;i += 2){
if(corners[i] < xtmin){
xtmin = corners[i];
}
}
ytmax = corners[1];
for(int i=1;i < corners.length;i += 2){
if(corners[i] > ytmax){
ytmax = corners[i];
}
}
ytmin = corners[1];
for(int i=1;i < corners.length;i += 2){
if(corners[i] < ytmin){
ytmin = corners[i];
}
}
}
public double getXTMax (){
return xtmax;
}
public double getYTMax () {
return ytmax;
}
public double getXTMin() {
return xtmin;
}
public double getYTMin(){
return ytmin;
}
//public static boolean isLeft (double x1, double y1, double x2, double y2, double x3, double y3){
//return ( 0 <= ( ( x2 - x1 ) * ( y - y1) )- (( y2 - y1) * ( x - x1)));
//}
public boolean isLeft1 (double randX, double randY){ // find if this point is to the left of the first line
return ( 0 <= ( ( corners[2] - corners[0] ) * ( randY - corners[1]) )- (( corners[3] - corners[1]) * ( randX - corners[0])));
}
public boolean isLeft2 (double randX, double randY){ // find if this point is to the left of the second line
return ( 0 <= ( ( corners[4] - corners[0]) * ( randY - corners[1]) )- (( corners[5] - corners[1]) * ( randX - corners[0])));
}
public boolean isLeft3 (double randX, double randY){ // find if this point is to the left of the third line
return ( 0 <= ( ( corners[4] - corners[2] ) * ( randY - corners[3]) )- (( corners[5] - corners[3]) * ( randX - corners[2])));
}
public boolean outsideTriangle ( double randX, double randY ){ // find if this point is inside of the triangle
int counter = 0;
if (isLeft1(randX,randY)){
counter++;
}
if (isLeft2(randX,randY)){
counter++;
}
if (isLeft3(randX,randY)){
counter++;
}
return counter == 2; // must be to the left of exactly 2 of the lines
}}
And then the AreaEstimator class:
public class AreaEstimator{
public static double[] maxAndMinValues (Circle[] circles,Triangle[] triangles){
// find maximum and minimum values according to all of the triangles and circles
double xmax = -100;
double ymax = -100;
double xmin = 100;
double ymin = 100;
for (int l=0; l < circles.length; l++){
if (xmax < circles[l].getXCMax()){
xmax = circles[l].getXCMax();
}
if (ymax < circles[l].getYCMax()){
ymax = circles[l].getYCMax();
}
if (xmin > circles[l].getXCMin()){
xmin = circles[l].getXCMin();
}
if (ymin > circles[l].getYCMin()){
ymin = circles[l].getYCMin();
}
}
for ( int m = 0; m < triangles.length; m++){
if(xmax < triangles[m].getXTMax()){
xmax = triangles[m].getXTMax();
}
if(ymax < triangles[m].getYTMax()){
ymax = triangles[m].getYTMax();
}
if(xmin > triangles[m].getXTMin()){
xmin = triangles[m].getXTMin();
}
if(ymin > triangles[m].getYTMin()){
ymin = triangles[m].getYTMin();
}
}
double[] result = new double [4];
result[0] = xmax;
result[1] = ymax;
result[2] = xmin;
result[3] = ymin;
return result;
}
public static void main (String[] args) {
double numThrows = Integer.parseInt(args[0]); // initialize amount of throws
if (numThrows <= 0){
throw new IllegalArgumentException();
}
int countCircles = 0; // find the amount of circles given
for ( int i = 1; i<args.length; i++){
if(args[i].equals("circle")){
countCircles++;
}
}
Circle[] circles = new Circle [countCircles];
for ( int i = 1; i<args.length; i++){
if (args[i].equals("circle")){
for ( int k = 0; k< countCircles; k++){
double xcenter = Double.parseDouble(args[i+1]);
double ycenter = Double.parseDouble(args[i+2]);
double radius = Double.parseDouble(args[i+3]);
circles[k] = new Circle (xcenter, ycenter, radius); //values associated with this circle
circles[k].maxAndMinCircle();//max and min value of the circle itself
}
}
}
int countTriangles = 0; // find the amount of the triangles given
for ( int i = 1; i < args.length; i++){
if(args[i].equals("triangle")){
countTriangles++;
}
}
Triangle[] triangles = new Triangle [countTriangles];
for ( int i = 1; i<args.length; i++){
if (args[i].equals("triangle")){
for ( int p = 0; p< countTriangles; p++){
double cornerx1 = Double.parseDouble(args[i+1]);
double cornery1 = Double.parseDouble(args[i+2]);
double cornerx2 = Double.parseDouble(args[i+3]);
double cornery2 = Double.parseDouble(args[i+4]);
double cornerx3 = Double.parseDouble(args[i+5]);
double cornery3 = Double.parseDouble(args[i+6]);
triangles[p] = new Triangle (cornerx1, cornery1, cornerx2, cornery2, cornerx3, cornery3);
//values associated with this triangle
triangles[p].maxAndMinTriangle(); //max and min value of the triangle itself
}
}
}
boolean dartsInOverlap = true;
double countInOverlap = 0; // initialize amount of darts in the overlapping shape
double[]result = maxAndMinValues(circles,triangles);
for(int i= 0;i < numThrows;i++){
double randX= (Math.random() * (result[0]-result[2]) + result[2]) ; // generate a random x value
double randY= (Math.random() * (result[1]-result[3])+ result[3]); // generate a random y value
for ( int h = 0; h < circles.length && dartsInOverlap; h++){
if (circles[h].outsideCircle(randX, randY)){
dartsInOverlap = false; // if the point is outside of the circle, it returns false
}
}
for ( int q = 0; q < triangles.length && dartsInOverlap; q++){
if (triangles[q].outsideTriangle(randX, randY)){
dartsInOverlap = false; // if the point is outside of the triangle, it returns false
}
}
if (dartsInOverlap){
countInOverlap++; // counts up the amount of points in the overlapping shape
}
dartsInOverlap = true;
}
System.out.println("This many darts were in the overlap between the shapes:" + countInOverlap);
// counts up the amount of points in the overlapping shape
System.out.println("The estimated overlapped areas is" + (result[0]-result[2])*(result[1]-result[3]) *(countInOverlap/numThrows));
//finds estimated area
}}
My code has worked for a few test cases, if in the command line there is only a circle and triangle entered, or only a sole circle. A triangle by itself or any other combination will give me answers far from the desired area estimation. I have looked and reviewed my code, and it all seems logical. So, where in my code could the problem persist? Any help would be appreciated.

There is at least a typo in maxAndMinTriangle. You wrote a 1 where you need a 0
xtmax = corners[0];
for(int i=1;i < corners.length;i += 2){
...
xtmin = corners[0];
for(int i=1;i < corners.length;i += 2){
it should be
xtmax = corners[0];
for(int i=0;i < corners.length;i += 2){
...
xtmin = corners[0];
for(int i=0;i < corners.length;i += 2){

Related

Rotate 2D array by alpha degrees

I wrote a function which takes two parameters:
JPG image as 3D array
rotation degrees given by alpha
My approach was:
public static int[][] rotate(int[][] img, double alpha) {
double rad = Math.toRadians(alpha);
double sin = Math.sin(rad);
double cos = Math.cos(rad);
int height = img.length;
int width = img[0].length;
int[][] rotate = new int[height][width];
for(int i = 0; i < height; i++) {
for(int j = height - i - 1; j < width; j++) {
if(j < height && i < width) {
double i_new = Math.floor(cos * (img[i].length - i) - sin * (img[j].length - j)) + i;
double j_new = Math.floor(sin * (img[i].length - i) + cos * (img[j].length - j)) + j;
rotate[i][j] = img[(int)j_new][(int)i_new];
}
}
}
return rotate;
}
While fixing the index range, the output is a black image. What am I missing?
After a while I got to a solution.
Caution: Its not using any special pre-defined libraries.
The global function which run`s over the matrice:
public static int[][] rotate(int[][] img, double alpha) {
double rad = Math.toRadians(alpha); //construct of the relevant angles
double sin = Math.sin(rad);
double cos = Math.cos(rad);
int height = img.length;
int width = img[0].length;
int[][] rotate = new int[height][width];
int a = height / 2; //we will use the area of a and b to compare coordinates by the formula given
int b = width / 2;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
double i_new = Math.floor(cos * (i - a) - sin * (j - b)) + a; // following the conversion function
double j_new = Math.floor(sin * (i - a) + cos * (j - b)) + b;
if (i_new >= rotate.length || i_new < 0 || j_new >= rotate[0].length || j_new < rotate[0][0]) { // if out of scope of the conversion --> print none
System.out.print(""); //mainly cause 'continue' statements are not necessary in java and JS
} else {
rotate[(int) i_new][(int) j_new] = img[i][j]; //convert
}
}
}
return rotate;
}
The global function which rotates each 2D matrice:
public static int[][][] rotate_alpha(int[][][] img, double alpha) {
int height = img[0].length;
int width = img[0][0].length;
int[][][] rotated = new int[3][height][width];
for (int k = 0; k < 3; k++) {
rotated[k] = rotate(img[k], alpha);
}
return rotated;
}
Hope this topic is solved by now, and stands by all the standards of the clean code.

Is there any way to improve performance of my procedural generation code?

I've been working on a project lately that creates procedurally generated terrain which uses 3d simplex noise as well as the Marching cubes algorithm. I am currently running it on my cpu which takes around 10-20 seconds to render a 200x200x200 terrain which isn't optimal if I want to make the world infinite. Is there any way to improve the speed which the terrain renders or is this the maximum speed I can achieve. (I've already tried using compute shaders but limitations with GLSL didn't allow me to pursue that idea.)
Terrain Generation Code
package Terrain;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import Entities.Entity;
import Maths.Vector3f;
import Models.RawModel;
import Render.Loader;
public class Terrain {
private int width = 200, height = 2, interval = 8;
private float x,y,z = 2f;
private int x1,y1,z1;
private Loader loader;
private List<Entity> cubes = new ArrayList<Entity>();
private RawModel model;
private double perlin2DScale = 0.01, perlin3DScale = 0.01;//, maskScale = 0.00;
private double perlin3Dratio = 0.8;// ratio of 3d noise to 2d;
private double amp = 1; //height of mountains
List<Vector3f> verticeArray = new ArrayList<Vector3f>();
float[][][] terrainMap = new float[width+1][height+1][width+1];
float[] SimplexMap3D = new float[(width+1)*(height+1)*(width+1)];
float[] SimplexMap2D = new float[(width+1)*(width+1)];
private float surfaceLevel = 1f;
int seed;
//SimplexComputeBuffer compute = new SimplexComputeBuffer();
public Terrain(float x, float z, Loader loader){
this.loader = loader;
this.x1 = (int) (x*width);
this.z1 = (int) (z*width);
this.x = x*width*interval;
this.z = z*width*interval;
Random rand = new Random();
seed = rand.nextInt(100000);
loadMarchingTerrain();
}
public void changeAmp(double i){
x+=i;
System.out.println("amp ="+amp);
verticeArray.clear();
loadMarchingTerrain();
}
public void changeSurface(double i){
surfaceLevel+=i;
System.out.println("surface ="+surfaceLevel);
verticeArray.clear();
loadMarchingTerrain();
}
public void loadMarchingTerrain(){
for(int x = x1; x<x1+width+1; x++){
for(int y = y1; y<y1+height+1; y++){
for(int z = z1; z<z1+width+1; z++){
double noise3d = this.sumOctaves(4,x,y,z,0.5,perlin3DScale,0,1); // creates 3d terrain like caves and overhangs
double noise2d = this.sumOctaves(4,x,z,0.5,perlin2DScale,0,1); // creates 2d terrain like mountains and hills (gives only height)
//double mask = this.sumOctaves(1,x,z,0.5,maskScale,0,1); // creates a 2d mask to vary heights of regions
float curHeight = (float)height*(float)(noise2d*(1-perlin3Dratio)+noise3d*perlin3Dratio); // mixing them together with correct ratio of 3d and 2d data
terrainMap[x-x1][y-y1][z-z1] = (float)-y+curHeight;
}
}
}
for(int x = 0; x<width; x++){
for(int y = 0; y<height; y++){
for(int z = 0; z<width; z++){
marchCube(new Vector3f(x,y,z));
}
}
}
float[] vertices = new float[verticeArray.size()*3];
int[] indice = new int[verticeArray.size()];
int vertexCount = 0;
for(Vector3f v : verticeArray){
vertices[vertexCount++] =v.x*interval;
vertices[vertexCount++] =v.y*interval;
vertices[vertexCount++] =v.z*interval;
}
for(int i = 0; i<indice.length; i++){
indice[i] = i;
}
model = loader.loadToVao(vertices, null, indice);
}
public int configIndex(float[] cube){
int configIndex = 0;
for(int i = 0; i<8; i++){
if(cube[i] > surfaceLevel){
configIndex |= 1 << i;
}
}
return configIndex;
}
public float sampleTerrain(Vector3f point){
return terrainMap[(int) point.x][(int) point.y][(int) point.z];
}
public Vector3f smoothPoint(Vector3f vert1, Vector3f vert2, int indice, float[] cube){
float sampleVert1 = cube[MarchingCubeTable.edges[indice][0]];
float sampleVert2 = cube[MarchingCubeTable.edges[indice][1]];
float difference = sampleVert1-sampleVert2;
if(difference == 0){
difference = surfaceLevel;
}else{
difference = (surfaceLevel-sampleVert1)/difference;
}
Vector3f a2 = vert1.subtract(vert2).scale(difference);
Vector3f vertPos = vert1.add(a2);
return vertPos;
}
public void marchCube(Vector3f position){
//create cube
float[] cube = new float[8];
for(int i = 0; i<8; i++){
Vector3f corner = position.add(MarchingCubeTable.cornerTable[i]);
cube[i] = terrainMap[(int) corner.x][(int) corner.y][(int) corner.z];
}
//search through index
int currentConfigIndex = configIndex(cube);
if(currentConfigIndex == 0 || currentConfigIndex == 255){
return;
}
//search through points
int edgeIndex = 0;
for(int j = 0; j<5; j++){
for(int i = 0; i<3; i++){
int indice = MarchingCubeTable.getIndex(currentConfigIndex)[edgeIndex];
if(indice == -1){
return;
}
Vector3f vert2 = position.add(MarchingCubeTable.cornerTable[MarchingCubeTable.edges[indice][0]]);
Vector3f vert1 = position.add(MarchingCubeTable.cornerTable[MarchingCubeTable.edges[indice][1]]);
Vector3f vertPos = this.smoothPoint(vert1, vert2, indice, cube);
verticeArray.add(vertPos);
edgeIndex++;
}
}
}
/*
* Simplex Noise functions
*/
public double sumOctaves(int iterations, double x, double y, double z, double persistance, double scale, double low, double high){
double maxamp = 0;
double amp = this.amp;
double frequency = scale;
double noise = 0;
for(int i = 0; i<iterations; i++){
noise += SimplexNoise.noise((x)*frequency, (y)*frequency, (z)*frequency)*amp;
maxamp += amp;
amp *= persistance;
frequency *= 2;
}
noise /= maxamp;
noise = noise * (high - low) / 2 + (high + low) / 2;
return noise;
}
public double sumOctaves(int iterations, int x, int y, double persistance, double scale, double low, double high){
double maxamp = 0;
double amp = this.amp;
double frequency = scale;
double noise = 0;
for(int i = 0; i<iterations; i++){
noise += SimplexNoise.noise((x)*frequency, (y)*frequency)*amp;
maxamp += amp;
amp *= persistance;
frequency *= 2;
}
noise /= maxamp;
noise = noise * (high - low) / 2 + (high + low) / 2;
return noise;
}
public List<Entity> getCubes(){
return cubes;
}
public float getX() {
return x;
}
public float getZ() {
return z;
}
public float getY() {
return y;
}
public RawModel getRawModel(){
return model;
}
}

Point in Polygon Android

I am developing an android application. In the application I have integrated map with polygon. Now I want to check whether the given marker is inside the polygon or not. Help Me please.
Following function is used to check the lat long in the polygon
pnpoly(no_of_vertex, lat, lng,(float) (point.latitude),(float) (point.longitude))
boolean pnpoly(int nvert, float vertx[], float verty[], float testx, float testy)
{
int i, j;
boolean c = false;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c =true;
}
return c;
}
I got the solution to my question following is the code
**private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
int intersectCount = 0;
for(int j=0; j<vertices.size()-1; j++) {
if( LineIntersect(tap, vertices.get(j), vertices.get(j+1)) ) {
intersectCount++;
}
}
return (intersectCount%2) == 1; // odd = inside, even = outside;
}
private boolean LineIntersect(LatLng tap, LatLng vertA, LatLng vertB) {
double aY = vertA.latitude;
double bY = vertB.latitude;
double aX = vertA.longitude;
double bX = vertB.longitude;
double pY = tap.latitude;
double pX = tap.longitude;
if ( (aY>pY && bY>pY) || (aY<pY && bY<pY) || (aX<pX && bX<pX) ) {
return false; }
double m = (aY-bY) / (aX-bX);
double bee = (-aX) * m + aY; // y = mx + b
double x = (pY - bee) / m;
return x > pX;
}**

Perlin Noise Generation not functioning properly

I have been working with perlin noise recently and when implementing it into a tile engine I am using, I have noticed that the perlin noise function produced "blocks" as seen in the picture below. Each pixel is another different location in a 500 by 500 array that is returned from the perlin noise function.
in this example the persistence is 0.5 with an octave count of 5
When playing with it further, the more octaves I have, the larger the block chunks.
Here is the Code that I am using to call the perlin noise function:
PerlinNoise p = new PerlinNoise();
//returns a float[][] array of 500 by 500
p.GeneratePerlinNoise(p.genWhiteNoise(500, 500), 5, (float) 0.1);
PerlinNoise class
import java.util.Random;
public class PerlinNoise {
Random r;
public PerlinNoise() {
r = new Random();
}
public void setSeed(long seed) {
r.setSeed(seed);
}
public void printOutArray(float[][] arr) {
for(int i = 0; i < arr.length; i++) {
for(int n = 0; n < arr[0].length; n++) {
System.out.print(arr[i][n] + ", ");
}
System.out.print("\n");
}
}
public void printOutTerrain(float[][] arr) {
for(int i = 0; i < arr.length; i++) {
for(int n = 0; n < arr[0].length; n++) {
float a = arr[i][n];
if(a < 0.4) {
System.out.print("W");
} else {
System.out.print("L");
}
}
System.out.print("\n");
}
}
//-------------------------------------------------------------//
float[][] genWhiteNoise(int width, int height) {
float[][] noise = new float[height][width];
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
noise[y][x] = r.nextFloat();
}
}
return noise;
}
float[][] genSmoothNoise(float[][] baseNoise, int octave) {
int height = baseNoise.length;
int width = baseNoise[0].length;
float[][] smoothNoise = new float[height][width];
int samplePeriod = 1 << octave; //calculates 2^k
float sampleFrequency = (float) (1.0/samplePeriod);
for(int i = 0; i < height; i++) {
int sample_i0 = (i / samplePeriod) * samplePeriod;
int sample_i1 = (sample_i0 + samplePeriod) % height; //wrap around
float vertical_blend = (i - sample_i0) * sampleFrequency;
for(int n = 0; n < width; n++) {
int sample_n0 = (n / samplePeriod) * samplePeriod;
int sample_n1 = (sample_n0 + samplePeriod) % width; //wrap around
float horizontal_blend = (n - sample_n0) * sampleFrequency;
//blend the top two corners
float top = Interpolate(baseNoise[sample_i0][sample_n0],
baseNoise[sample_i1][sample_n0], horizontal_blend);
//blend the bottom two corners
float bottom = Interpolate(baseNoise[sample_i0][sample_n1],
baseNoise[sample_i1][sample_n1], horizontal_blend);
//final blend
smoothNoise[i][n] = Interpolate(top, bottom, vertical_blend);
}
}
return smoothNoise;
}
float[][] GeneratePerlinNoise(float[][] baseNoise, int octaveCount, float persistance)
{
int height = baseNoise.length;
int width = baseNoise[0].length;
float[][][] smoothNoise = new float[octaveCount][][]; //an array of 2D arrays containing
//generate smooth noise
for (int i = 0; i < octaveCount; i++)
{
smoothNoise[i] = genSmoothNoise(baseNoise, i);
}
float[][] perlinNoise = new float[height][width];
float amplitude = 1.0f;
float totalAmplitude = 0.0f;
//blend noise together
for (int octave = octaveCount - 1; octave >= 0; octave--)
{
amplitude *= persistance;
totalAmplitude += amplitude;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
}
}
}
//normalisation
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
perlinNoise[i][j] /= totalAmplitude;
}
}
return perlinNoise;
}
//linear average between two points
float Interpolate(float x0, float x1, float alpha)
{
return Cosine_Interpolate(x0, x1, alpha);
}
//Linear Interpolation
float Linear_Interpolate(float x0, float x1, float alpha)
{
return x0 * (1 - alpha) + alpha * x1;
}
//Cosine interpolation (much smoother)
float Cosine_Interpolate(float x0, float x1, float alpha)
{
float ft = (float) (alpha * 3.141592653589);
float f = (float) ((1 - Math.cos(ft)) * 0.5);
return x0*(1-f) + x1*f;
}
}
So to reiterate my question: Why is my perlin noise function behaving the way it does, as in only generating space in chunks?
So to fix this, all I had to do is swap the vertical_blend and horizontal_blend variables in the genSmoothNoise() method. It's amazing what you notice after a break

use 2 dimension array java to store the point(x,y)

I am trying to write a program like bouncingBall. but i generated N obstacles in the screen. Each time the ball touch the obstacle, the obstacle disappears and shows up at another random place. i am trying to use 2 dimension array to store the random-gernerated obstacles' point (x,y).
Right now if I input N>50, it gives me outofbound.
But what i want is to store point from (0,0) to (50,50)..what should I do achieve this with 2-dimentional array?
Thanks!
import java.util.ArrayList;
public class BouncingBall {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java BouncingBall N");
System.exit(0);
}
int N = Integer.parseInt(args[0]);
if (N > 2500) {
System.out.println("Usage: java BouncingBall N<=2500");
System.exit(0);
}
double[][] myArray = new double[50][50];
// set the scale of the coordinate system
StdDraw.setXscale(-1.0, 1.0);
StdDraw.setYscale(-1.0, 1.0);
// initial values
double rx = 0.480, ry = 0.860; // position
double vx = 0.015, vy = 0.023; // velocity
double radius = 0.02; // radius
double x;
double y;
double a[] = new double[2];
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledSquare(0, 0, 1.0);
StdDraw.setPenColor(StdDraw.BLACK);
for(int i=0; i <= N; i++){
x = 2.0*(double)Math.random()-1.0;
y = 2.0*(double)Math.random()-1.0;
for (int t=0;t <50;t++){
for (int j=0;j <50;j++){
myArray[t][j]= x;
myArray[j][t]= y;
}
}
StdDraw.filledSquare(x, y, 0.02);
}
// main animation loop
while (true) {
// bounce off wall according to law of elastic collision
if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx;
if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy;
// clear the background
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledSquare(0, 0, 1.0);
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
for(int t=0; t <= N; t++){
for (int j=0;j <50;j++){
x = myArray[t][j];
y = myArray[j][t];
}
if ((Math.abs(rx + vx) > x - radius)||(Math.abs(ry + vy) > y - radius))
{ //if the ball touch the square
vx = -vx;
vy = -vy;
if (args.length == 2 && args[1].equals("-d")){
x = 2.0*(double)Math.random()-1.0; //new random x
y = 2.0*(double)Math.random()-1.0; //new random y
}else{
;
}
StdDraw.filledSquare(x, y, 0.02);
}
else{
StdDraw.filledSquare(x, y, 0.02); //if not touched, keep it.
}
}
rx = rx + vx;
ry = ry + vy;
StdDraw.filledCircle(rx, ry, radius);
// display and pause for 20 ms
StdDraw.show(20);
}
}
}
Imagine that the user inputs -1 for N, then x and y won't get a value because the loops bodies won't run.
Simple workaround: assign a default value to x and y (0 for example)
double x = 0;
double y = 0;
You need to initialize both x and y:
double x = 0;
double y = 0;
Your ArrayIndexOutOfBoundsException is occurring because you only define the array to be 50x50
double[][] myArray = new double[50][50];
yet access an index greater than that using t:
x = myArray[t][j];
you have to initialize your local variables, local variables dont get default values
int double x=0.0;
int double y=0.0;
would solve the compiler error.
if N>50
for (int t=0;t <50;t++){
for (int j=0;j <50;j++){
myArray[t][j]= x; // ArrayIndexOutOfBound Exection occurs here
myArray[j][t]= y;
}
}

Categories