0

i wanna create pyramid left,right,center angle, using Circle shape in GridPane, i have already done with the left angle, but spent 2 days in creating right and center angle with no luck.

i will be thankful if anyone knows or can give me some algo ideas please help me!

Output already done left angle

enter image description here

With the following code

        int i, j;
        for (i = 0; i < n; i++) {
            for (j = 0; j <= i; j++) {
                Circle circle = new Circle();
                circle.setStroke(Paint.valueOf(Color.BLACK.toString()));
                circle.radiusProperty().bind(ballsModel.radiusProperty());
                circle.strokeWidthProperty().bind(ballsModel.strokeProperty());
                circle.fillProperty().bind(Bindings.createObjectBinding(() -> Paint.valueOf(ballsModel.getColor().name())));
                grid.addRow(i, circle);
            }
        }

Need to figure out following patterns:

center angle enter image description here

right angle enter image description here

Steve Piercy
  • 13,693
  • 1
  • 44
  • 57
  • Hint: calculate the indices where to place the circles; don't try to use `addRow` or `addColumn`, but `add(Node, int column, int row)`. `addColumn`/`addRow` won't work for adding children, if there are "cells" with smaller indices to remain empty, such as the cricle at (1, 3) in the last picture; I'm not going to solve your homework for you btw. It shouldn't be too hard to figure out the correct index ranges though. – fabian May 15 '20 at 16:59
  • @fabian that's what i am trying to do since 2 days, but i need some illustration code to accomplish – Salman Sabir May 15 '20 at 17:10
  • Maybe trying to create inequations for the sides of the triangle helps? `x >= 0`, `y <= 4` and `y >= x` for the solved problem; this implies `y >= 0` (combining first and last equation, in case that's not obvious), so you can create an outer loop `for (int y = 0; y <= 4; ++y) { ... }`; now given y you can derive the valid range for `x` combining `x >= 0` and `y >= x` for `0 <= x <= y` resulting in an inner loop of `for (int x = 0; x <= y; ++x) { ... }` For coming up with the inequations the [two points form](https://mathworld.wolfram.com/Two-PointForm.html) should help. – fabian May 15 '20 at 18:02
  • Alternatively at least for the last shape you can think of this being the first shape mirrored on the vertical line going through the center, i.e. points can be transformed as `(x, y) -> (k-x, y)` for a suitable constant `k`... The second shape can be thought of as the combination of the last one and a slightly smaller version of the first one moved down and right, but if you can determine the upper or the lower bound for x based on y you've you may be able to determine the other bound based on the symetry... – fabian May 15 '20 at 18:31

1 Answers1

0

an example for the pyramid part

package pyramid;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;


public class Pyramid extends Application {



   void createPyramid(){
   gridPane.setHgap(5);
   gridPane.setVgap(5);
   int center = 4 ;


   for (int row = 0; row <= 4; row++ ) {

      int  range = (1 +(2* row));

      int  startColumn = center-range/2 ;

       for(int i = 0 ; i<range; i++){
           Circle circle  = new Circle(20,javafx.scene.paint.Color.BLUE);
           gridPane.add(circle,startColumn+i , row);

       }     

   }} 

@Override
public void start(Stage primaryStage) {


    StackPane root = new StackPane();
    root.getChildren().add(gridPane);
    this.createPyramid();

    Scene scene = new Scene(root, 600, 600);

    primaryStage.setTitle("Pyramid");
    primaryStage.setScene(scene);
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}}

enter image description here

Giovanni Contreras
  • 2,345
  • 1
  • 13
  • 22
  • awesome! thank you very much buddy, it solved my problem, but there is 1 more i can't get rid of right one, i have to submit this assignment today evening, well i really appreciate your help, now only have to focus on right angle. – Salman Sabir May 15 '20 at 22:31
  • a GridPane is like a two dimensional array the position of the first circle in row 0 is column 4 . in each iteration the amount of circles increaces by one – Giovanni Contreras May 16 '20 at 01:06