Need some Java help.

overlord20

Gawd
Joined
Feb 5, 2010
Messages
623
I need some help with a bit of my code. I posted all my objects so far. I need to store line segments in _poly in the polygon object. I figured that out. But it takes points x,y and x1,y1 for the first line segment and then x2,y2, x3,y3 for the second line segment.

I need it to take x,y, and x1,y1, for the first and x1,y1 and x2,y2 for the second and x2,y2, x3,y3 for the third and so and so forth to the amount of coordinates I have. the last one should be x5,y5 - x,y.

I hope that makes sense. I think a loop of some sort would work. I am just stumped.

Not looking for answer but help to get towards that answer. So as simple or detailed as possible please if you can help.

Thanks in advances. I included all the code, mainly the Forest, Polygons, Lines, and Coord objects.

Code:
import java.util.*;

public class project2.java
{
    public static void main(String[] args) throws IOException
    {
        Scanner scan = new Scanner(System.in);
        String infilename, outfilename;
        int i,howmany, nump, numdd, numhw;
        int maxp, maxdd, maxhw;
        double sx, sy, ex, ey;
        
        if ( args.length != 2 )
        {
            System.out.print("Enter input map name: ");
            infilename = scan.nextLine();
            System.out.print("Enter output map name: ");
            outfilename = scan.nextLine();
        }
        else
        {
            infilename = args[0];
            outfilename = args[1];
        }
        
        Scanner file = new Scanner(new FileReader(infilename));
        PrintWriter fout = new PrintWriter(outfilename);
        
        
        
        System.out.println("Enter the Start Coordinates");
        System.out.print("X: ");
        sx = scan.nextDouble();
        System.out.print("Y: ");
        sy = scan.nextDouble();
        
        System.out.println("Enter the End Coordinates");
        System.out.print("X: ");
        ex = scan.nextDouble();
        System.out.print("Y: ");
        ey = scan.nextDouble();
        
        
        
        
        
    }
    
}
Forest Object
Code:
import java.util.*;

public class Forest
{
    protected Polygons[] obstacles;
    protected Coord[] _path;
    
    public Forest(Scanner file)
    {
        howmany = file.nextInt();
        
        obstacles = new Polygons[howmany];
        
        for ( i = 0; i < howmany.length; i ++ )
        {
            obstacles[i] = new Polygons(file);
            obstacles[i].Polygons(file);
        }
    }
    
    public void route(int howmany)
    {
        int i;
        
        _path = new Coord[random + 2];
        
        _path[0].start(scan);
        
        for (i = 0; i < howmany; i++)
            _path[i].route(gen, howmany, double sx, double sy,
                    double ex, double ey)
        
        _path[].end(scan);
        
        
    }    
    
}
Polygon Object
Code:
import java.util.*;

public class Polygons
{
    protected Lines[] _poly;
    
    public Polygons(Scanner file)
    {
        int howmany_sides, i;
        
        howmany_sides = file.nextInt();
            
        _poly = new Lines[howmany_sides];
        
        for ( i = 0; i < howmany_sides.length; i ++ )
        {
            _poly[i] = new Lines(file);
            _poly[i].Lines(file);
        }
        
    }
    
    public boolean check()
    {
        
        
    }
}
Line Object
Code:
import java.util.*;

public class Lines
{
//    protected Coord[] _sides;
    protected double _m, _b;
    protected Coord p1, p2;
    
    public Lines(Scanner file)
    {
        p1 = Coord(file);
        p2 = Coord(file);
        
        _m = p2.y-p1.y/p2.x-p1.x;
        _b = p1.y - _m * p1.x;
    }
    
    public void path()
    {
        _m = p2.y-p1.y/p2.x-p1.x;
        _b = p1.y - _m * p1.x;
    }
}
Coord Object
Code:
import java.util.*;

public class Coord
{
    protected double x, y;
    protected double px, py;
    
    //    Name: Coord
    //    Desc: Holds an array of coordinates.
    //    Params: Polygons obstacles
    //    Return: N/A
    //    1) get obstacles from Polygons.
    //    2) put obsticles in array "obstacles"
    public Coord(Scanner file)
    {
        int x, y;
        
        x = file.nextDouble();
        y = file.nextDouble();
        
    }
    
    //    Name:     route
    //    Desc:     Generates coordinates that make up a route from start to
    //        end. If coordinate intersects with a polygon from Map,
    //        generate a new point.
    //    Params: obsticles
    //    Return: void
    //    3) Generate a random point.
    //    4) Draw line from previous to new random point.
    //    5) if line interects with polygon generate new random point.
    //    6) once line doesn't intersect with polygon repeate 3-6.
    public void route(Random gen, double sx, double sy,
        double ex, double ey)
    {
        px = gen.nextDouble((ex - sx)+sx);
        py = gen.nextDouble((ey - sy)+sy);
    }
    
    public void p1(Polygons[] _poly)
    {
        x = _poly[i];
        y = _poly[i];
    }
    public void p2(Polygons[] _poly)
    {
        x = _poly[i];
        y = _poly[i];
    }
    
}
 
I need some help with a bit of my code. I posted all my objects so far. I need to store line segments in _poly in the polygon object. I figured that out. But it takes points x,y and x1,y1 for the first line segment and then x2,y2, x3,y3 for the second line segment.

I need it to take x,y, and x1,y1, for the first and x1,y1 and x2,y2 for the second and x2,y2, x3,y3 for the third and so and so forth to the amount of coordinates I have. the last one should be x5,y5 - x,y.

I hope that makes sense. I think a loop of some sort would work. I am just stumped.

its not clear to me what you mean but:

if you have variables x1, x2, x3, you can't (easily) do a loop over those variables.

if you have an array of x, then you can easily loop over the array.

with both x's and y's, you may want a 2 dimensional array
int[][] xyPoints = new int[6][2]; //for example

then you can write a for loop:
Code:
for(int p=0; p<xyPoints.length; p++) {
  x_p=xyPoints[p][0];
  y_p=xyPoints[p][1];
}
 
Back
Top