How to stretch image according to vertices | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to stretch image according to vertices

I have an array of 2D points which represent a polygon and I have a buffered image, how do I implement it so that the image STRETCHES around each point?

25th Dec 2020, 1:23 AM
Frumkin
Frumkin - avatar
1 Answer
+ 1
It is hard to know what you want. If you want a solid colour filling in the whole polygon and want to ensure the BufferedImage is large enough to cover the entire polygon, you can calculate the minimum dimensions by looping through all vertices to get the minimum and maximum x and y coordinates. If any minimums are negative, you'll need to translate the polygon so the minimum coordinate needed is at least 0. The width would need to be at least max-min x coordinate. The height would need to be at least max - min y coordinate. If you have an image and want to draw it while treating the polygon like a mask, you can use the Graphics class's setClip method. You should be able to make a Polygon( https://docs.oracle.com/javase/7/docs/api/java/awt/Polygon.html ) from your vertices and pass that to the setClip method before drawing your image with drawImage. It would be more difficult if your polygon has holes or islands.
25th Dec 2020, 9:49 PM
Josh Greig
Josh Greig - avatar