I would like to create such a Shape.
Just draw a Circle and then Lines.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class SkipPartsOfCycle extends Application {
@Override
public void start(Stage stage) {
var pane = new Pane();
pane.getChildren().add(new Circle(500, 500, 100, Color.GREEN));
drawLine(pane, 0, 20, 5);
drawLine(pane, 70, 110, 5);
drawLine(pane, 160, 200, 5);
drawLine(pane, 250, 290, 5);
drawLine(pane, 340, 360, 5);
stage.setScene(new Scene(pane, 1000, 1000));
stage.show();
}
public static void drawLine (Pane pane, int start, int end, int increment) {
for (var i = start; i <= end; i += increment) {
pane.getChildren().add(new Line(500,500, 500 + (100 * Math.sin(Math.toRadians(i))), 500 + (100 * Math.cos(Math.toRadians(i)))));
}
}
public static void main(String[] args) {
launch(args);
}
}
This code works. The problem is that to skip specific parts of a cycle, I call it several times with a specific range. Another solution is to insert the condition into the cycle, but it would be unnecessarily complicated.
The biggest problem is that since I call it several times, when changing (eg 0-30,60-120,150-210,240-300,330-360) it is necessary to modify all method calls and not just one.
Is there a simpler solution, please?
You can boil your problem down to drawing lines whenever the steps of your loop fall within 20 degrees before or after angles measuring 0, 90, 180 and 270 degrees, so some branching logic should let you draw your figure with a single pass.
In truth, creating a method and calling it five times isn't a big deal in terms of overhead, not to mention making your code somewhat more flexible, so your solution isn't actually bad.
for (var i = 0; i <= 360; i += 5) {
if((i <= 20) ||
(i >= 70 && i <= 110) ||
(i >= 160 && i <= 200) ||
(i >= 250 && i <= 290)
(i >= 340))
{
pane.getChildren().add(new Line(500,500, 500 + (100 * Math.sin(Math.toRadians(i))), 500 + (100 * Math.cos(Math.toRadians(i)))));
}
}