简体   繁体   中英

drawing shapes on a grid

I need to draw random shapes on a grid such as lines squares etc. This part I'm able to do. My problem is the start and end point of the lines I'm drawing falls anywhere in a grid cell. I would like them to be only at intersection points. One cell in the grid is a 10x10 pixel grid. Do i have to write an algorithm to assign the pixel to its nearest intersection point on the grid or is there a easier way. I'm using a buffered image to draw the grid. Please Help. this is what i have so far

for (int i = 0; i < 61; i++) {
                g2d.drawLine((imgDim.width + 2) / 40 * i, 0,
                        (imgDim.width + 2) / 40 * i, imgDim.height - 1);
                g2d.drawLine(0, (imgDim.height + 2) / 60 * i,
                        imgDim.width - 1, (imgDim.height + 2) / 60 * i);
            }

Thank you

How are you coming up with the random points? Making an adjustment there might be the easiest way. That is, just drop a 0 in the process you are using to come up with the points in the first place. Then when you are ready to draw it, add a 0 back.

Seriously? In order to make a random point (pixelX, pixelY) snap to the closest point of a grid.

int gridSize = 10;
int x = (pixelX + gridSize / 2) / gridSize * gridSize;
int y = (pixelY + gridSize / 2) / gridSize * gridSize;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM