gpt4 book ai didi

java - 使用 addRectangle 将矩形转换为多边形

转载 作者:行者123 更新时间:2023-11-30 07:46:01 26 4
gpt4 key购买 nike

如何使用以下方法将矩形转换为多边形?

public void addRectangle(int xPos, int yPos, int dX, int dY) 

我已经尝试过:

/** * Converts the rectangle supplied into a polygon by making a new polygon 
and adding each of the rectangle's corners as points. */

public static Polygon RectangleToPolygon(Rectangle rect) {
Polygon result = new Polygon();
result.addPoint(rect.x, rect.y);
result.addPoint(rect.x + rect.width, rect.y);
result.addPoint(rect.x + rect.width, rect.y + rect.height);
result.addPoint(rect.x, rect.y + rect.height);
return result;
}

最佳答案

最好对所有数据使用构造函数。

public static Polygon RectangleToPolygon(Rectangle rect) {
int[] xpoints = {rect.x, rect.x + rect.width, rect.x + rect.width, rect.x}:
int[] ypoints = {rect.y, rect.y, rect.y + rect.height, rect.y + rect.height};
return new Polygon(xpoints, ypoints, 4);
}

或者将矩形的x、y、宽度、高度替换为

int xPos, int yPos, int dX, int dY

关于java - 使用 addRectangle 将矩形转换为多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33958145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com