简体   繁体   中英

Program giving strange output

This program is meant to output a series of integers which can be read by another program (provided by my instructor; I am certain that her code is not the issue) to draw a pattern of lines.

The intended pattern is four curves which start at each corner and rotate counter-clockwise towards the center; then stops when the length of each movement is greater than the distance between one point and the next.

Looks like this:
http://i.imgur.com/fLeHZah.png?1

Unfortunately, the program either stops when this distance is reached by the first point, and doesn't allow the other three to complete, or it stops when the fourth and final point has reached this point, but in the meantime has output a bunch of other random lines that don't make any sense.

Looks like this:
http://i.imgur.com/1ncJquj.png?1

Any insight would be appreciated at this point. Code is below.

import java.util.Scanner;
import java.io.*;
import java.awt.Point;

public class Slugs {
    public static void main(String[] args) {
        Scanner input = null;
        try {
            input = new Scanner(new File("slug_details.txt"));
        }
        catch (java.io.FileNotFoundException e) { 
            System.out.println("Sorry, file not found.");
            System.exit(-1);
        }
        String filename = input.next();
        int boxSize = input.nextInt();
        int d = input.nextInt();

        Point[]slugs = new Point[4];
        slugs[0] = new Point(0, 0);
        slugs[1] = new Point(0, boxSize);
        slugs[2] = new Point(boxSize, boxSize);
        slugs[3] = new Point(boxSize, 0);

        PrintStream output = null;
        try {
            output = new PrintStream(new File(filename));
        }
        catch (java.io.FileNotFoundException e) {
            System.out.println("Sorry, file could not be created.");
            System.exit(-1);
        }
        output.println(boxSize + " " + boxSize);

        moveSomeSlugs(slugs, d, output);

    }
    public static void moveSomeSlugs(Point[]slugs, double d, PrintStream output){
        /* move slugs distance d until d >
         */
        double distance = 0.0;
        double ratio = 0.0;
        for (int i = 0; i<(slugs.length); i++){
            distance = Math.sqrt((Math.pow((slugs[(i+1)%4].x-slugs[i].x), 2))+(Math.pow((slugs[(i+1)%4].y-slugs[i].y), 2)));
            ratio = d/distance;
        }
        while (d <= distance){
            for (int i = 0; i<4; i++){
                double xmoveDist = (slugs[(i+1)%4].x-slugs[i].x)*ratio;
                double ymoveDist = (slugs[(i+1)%4].y-slugs[i].y)*ratio;
                output.print (Math.abs(slugs[i].x) + " " + Math.abs(slugs[i].y) + " ");
                slugs[i].x += xmoveDist;
                slugs[i].y += ymoveDist;
                output.println (Math.abs(slugs[i].x) + " " + Math.abs(slugs[i].y));
                distance = Math.sqrt((Math.pow((slugs[(i+1)%4].x-slugs[i].x), 2))+(Math.pow((slugs[(i+1)%4].y-slugs[i].y), 2)));
                ratio = d/distance;
                if (d > distance && i == slugs.length-1){
                    break;
                }
            }
        }
    }
}

It looks to me like you are suffering from severe rounding/truncation errors in your calculations, due (largely) to the fact that you are using int to represent the definitive XY locations of the "slugs". (That is the x and y values of the Point objects.)

This means that you are converting from int to double and back to int repeatedly. Each conversion from double to int introduces an error,

I suggest you recode this using double to represent the true XY locations, and just convert to int values when you draw the lines.


(There may be something else going on here as well; ie some other bug that is less obvious. But doing the calculations the way I suggest will remove some "noise" AND make your code easier to understand.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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