0

I'm new to java and I want to change the size of my polygon while maintaining the aspect ratio. How do I scale up my shape without needing to change all the vertices manually 1 by 1? I would like to just say something like gc.scale(10); and it makes the polygon 10x better.

    public void draw(GraphicsContext gc) {


        double[] xVertices = {0.0,  0.0, 10.0,  10.0,  20.0, 20.0, 30.0,  30.0,  40.0, 40.0, 50.0,  50.0,  60.0, 60.0, 70.0, 70.0};
        double[] yVertices = {0.0, 10.0, 10.0, 100.0, 100.0, 90.0, 90.0, 100.0, 100.0, 90.0, 90.0, 100.0, 100.0, 10.0, 10.0,  0.0};


        gc.setFill(color);
        gc.translate(x, y);
        gc.rotate(180);
        gc.fillPolygon(xVertices, yVertices, 16);

    }
Raethexn
  • 21
  • 2
  • You can define a function that finds the center of the polygon and scales the difference from the center for each point. I'm not aware of a prebuilt function. You can use the map function from functional java also. – kpie Feb 16 '22 at 04:16
  • Are you sure you should be using a canvas rather than the scene graph? This kind of thing is relatively trivial using the scene graph. – jewelsea Feb 16 '22 at 05:05
  • For a canvas, the implementation will be similar to [this solution for rotating stuff drawn on the canvas](https://stackoverflow.com/questions/18260421/how-to-draw-image-rotated-on-javafx-canvas), except that you apply a scale transform to the graphics context rather than a rotate transform. – jewelsea Feb 16 '22 at 05:08
  • 1
    Study the basics of [2D transforms](https://www.cs.uic.edu/~jbell/CourseNotes/ComputerGraphics/2DTransforms.html) and the JavaFX transform APIs (rotate, scale, concatenate, etc) and GraphicsContext API if you don’t already know that fundamental stuff. Make sure you understand [pivots](https://web.cse.ohio-state.edu/~shen.94/681/Site/Slides_files/transformation_review.pdf). – jewelsea Feb 16 '22 at 05:14
  • You can [scale the canvas node itself](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/Node.html#setScaleX(double)), though that likely isn't what you want, as that will scale the whole canvas (and everything in it), not just the next polygon you draw. – jewelsea Feb 16 '22 at 08:54
  • 1
    "I would like to just say something like gc.scale(10);" -> but you can, [`gc.scale(10, 10)`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/canvas/GraphicsContext.html#scale(double,double)), note though, that that, on its own, may not be exactly what you really want, you might want to pivot (translate to the origin, scale, translate back to the original location), which you can learn about through the resources in the prior comment. – jewelsea Feb 16 '22 at 08:54
  • Two approaches are mentioned [here](https://stackoverflow.com/a/70945900/230513) in the context of a related `Shape`, `SVGPath`. – trashgod Feb 16 '22 at 15:59
  • @kpie *"I'm not aware of a prebuilt function."* There's no single method, but `Point2D` supports enough to make it easy. If `center` and `vertex` are the relevant `Point2D` instances, the new vertex is `center.add(vertex.subtract(center).multiply(scale))`. – James_D Feb 16 '22 at 18:04
  • @jewelsea I ended up deciding to make a for loop that adds values to the coordinates in order to uniformly move the polygon without messing up the rest of the drawings. When I tried Scale(x, y); it would mess up any remaining drawing I had to do. Thank you for the help – Raethexn Feb 17 '22 at 01:17
  • If you have a solution, you can answer your own question if you want. – jewelsea Feb 17 '22 at 04:43

0 Answers0