Search code examples
javafor-loopif-statementgraphicsformula

Java: Formula for evenly distributing lines depending on how many lines are added, in 4 Quadrants


Question: Hey there, I'm working on a Java code to create a sort of Parabolic Star consisting of straight lines in a canvas of the size 400x400. Can someone help me? Here is what this graph should look like and down below I'll provide you with the code i got so far: Graph with the values: a) numLinesQuadrant = 2, b) numLinesQuadrant = 5, c) numLinesQuadrant = 11, d) numLinesQuadrant = 21

My Java code: import codedraw.CodeDraw; import codedraw.Palette;

public class TryOutTwo { public static void main(String[] args) {

    CodeDraw myDrawObj = new CodeDraw(400,  400);
    //add range?
    int numlinesQuadrant = 2;
    int xSpacing = 200 / (numlinesQuadrant);
    int ySpacing = 200 / (numlinesQuadrant);

    for (int i = 0; i < numlinesQuadrant; i++) {
        boolean isEven = (i % 2 == 0);
                int startX = 200 + (i * xSpacing);
                int startY = 200 + (i * ySpacing);
                int endX = 200 - (i * xSpacing);
                int endY = 200 + (i * ySpacing);
                myDrawObj.setColor(Palette.GREEN);
                if (isEven) {
                    myDrawObj.drawLine(200, startY, endX, 200);
                } else {
                    myDrawObj.drawLine(startX, 200, 200, endY);
                }
                int startX2 = 200 + (i * xSpacing);
                int startY2 = 200 + (i * ySpacing);
                int endX2 = 200 + (i * xSpacing);
                int endY2 = 200 + (i * ySpacing);
                myDrawObj.setColor(Palette.CYAN);
                if (isEven) {
                    myDrawObj.drawLine(200, startY2, endX2, 200);
                } else {
                    myDrawObj.drawLine(endX2,200,200,endY2);
                    }
                int startX3 = 200 - (i * xSpacing);
                int startY3 = 200 - (i * ySpacing);
                int endX3 = 200 - (i * xSpacing);
                int endY3 = 200 - (i * ySpacing);
                myDrawObj.setColor(Palette.ORANGE);
                if (isEven) {
                    myDrawObj.drawLine(200, startY3, endX3, 200);
                } else {
                    myDrawObj.drawLine(startX3, 200, 200, endY3);
                }
                int startX4 = 200 + (i * xSpacing);
                int startY4 = 200 - (i * ySpacing);
                int endX4 = 200 + (i * xSpacing);
                int endY4 = 200 - (i * ySpacing);
                myDrawObj.setColor(Palette.MAGENTA);
                if (isEven) {
                    myDrawObj.drawLine(200, startY4, endX4, 200);
                } else {
                    myDrawObj.drawLine(startX4, 200, 200, endY4);
                }
        }

    myDrawObj.setColor(Palette.BLACK);
    myDrawObj.setLineWidth(2);
    myDrawObj.drawLine(0, 200, 400, 200);
    myDrawObj.drawLine(200, 0, 200, 400);

    myDrawObj.show();
}

}

Also, here is my Task Description if it helps: Here's what I need to do:

  • Modify the main method to achieve the following:

    • Generate images similar to the ones in the figures. Each run of the program should produce a single image.
    • The images should be 400×400 pixels in size, with point P (x = 0, y = 0) positioned at the upper-left corner. Use CodeDraw myDrawObj = new CodeDraw(400, 400) to create the drawing window.
  • Additional instructions:

    • Introduce a variable numLinesQuadrant of type int to determine the number of lines per quadrant. The spacing between the endpoints of the lines on each axis should depend on this variable. No need for input from the command line.
    • Utilize a single loop to draw the entire image.
    • Set the drawing color to GREEN, CYAN, MAGENTA, or ORANGE using myDrawObj.setColor(Palette.GREEN) and similar methods.
    • After the loop, draw a black horizontal and vertical line at the center of the image, both 2 pixels thick (myDrawObj.setLineWidth(2)).
  • Btw I'm using the 'CodeDraw' Documentation: https://krassnig.github.io/CodeDrawJavaDoc/v4.0.x/codedraw/CodeDraw.html

I just can't seem to figure out a formula for evenly distributing the start and end points of each line along the axis. It gets messy when I add more lines. There are 2 types of lines in each quadrant. 2 of the 4 coordinates need to be flexible for calculation, the other two can stay the same. Atleast i think so. And for them to move accordingly, and spread out evenly i need a solid formula that i cannot figure out for some reason.


Solution

  • Here is a JavaFX version.

    The key is to move the lines proportionally. Basically, the lines lengths are the same, but their position on the x and y axis is different.

    Try loopMax = 3, loopMax = 6, loopMax = 12, and loopMax = 24.

    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
     
    public class App extends Application {
     
        double viewWidth = 500.0;
        double viewHeight = 500.0;
        double minX = 0.0;
        double midX = viewWidth / 2.0;
        double maxX = viewWidth;
        double minY = 0;
        double midY = viewHeight / 2.0;
        double maxY = viewHeight;
        double loopMax = 24.0;
        
        
        public static void main(String[] args) {
            launch(args);
        }
     
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("Drawing Operations Test");
            Group root = new Group();
            Canvas canvas = new Canvas(viewWidth, viewHeight);
            GraphicsContext gc = canvas.getGraphicsContext2D();
            
            root.getChildren().add(canvas);
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
            
            drawAxisX(gc);
            drawAxisY(gc);
            double fraction = 0;
            for(int i = 1; i <= loopMax ; i++)
            {
                drawShape(gc, fraction);
                System.out.println(fraction);
                fraction = i / loopMax;
            }
            
        }
    
        private void drawAxisX(GraphicsContext gc) {
            gc.setFill(Color.BLACK);
            gc.setStroke(Color.BLACK);
            gc.setLineWidth(5);
            gc.strokeLine(minX, midY, maxX, midY);        
        }
        
         private void drawAxisY(GraphicsContext gc) {
            gc.setFill(Color.BLACK);
            gc.setStroke(Color.BLACK);
            gc.setLineWidth(5);
            gc.strokeLine(midX, minY, midX, maxY);        
        }
         
         private void drawShape(GraphicsContext gc,double fraction)
         {
            gc.setFill(Color.GREEN);
            gc.setStroke(Color.GREEN);
            gc.setLineWidth(1);
            gc.strokeLine(minX + (midX * fraction), midY, midX, minY + (midY * (1 - fraction))); //upper /
            
            
            gc.setFill(Color.ORANGE);
            gc.setStroke(Color.ORANGE);
            gc.strokeLine(minX + (midX * fraction), midY, midX, maxY - (midY * (1 - fraction)));
            
            gc.setFill(Color.VIOLET);
            gc.setStroke(Color.VIOLET);
            gc.strokeLine(maxX - (midX * fraction), midY, midX, maxY - (midY * (1 - fraction)));
            
            gc.setFill(Color.CYAN);
            gc.setStroke(Color.CYAN);
            gc.strokeLine(maxX - (midX * fraction), midY, midX, minY + (midY * (1 - fraction)));        
         }
    }
    

    Output

    enter image description here