gpt4 book ai didi

java,判断圆是否在一个区域内

转载 作者:行者123 更新时间:2023-12-01 13:18:12 27 4
gpt4 key购买 nike

嗨,我是编程新手,我正在尝试用 java 编写一个算法来确定圆是否在矩形区域中

我有圆的半径和圆中间的点(中心)

|_____________________________________________________
|
|
|
| circle
|
|
|
|
|(0,0)________________________________________________

左下角代表坐标(0,0)

这是我到目前为止所拥有的,但我知道我在某个地方有一个我找不到的错误

if (mCenter.getmX() + mRadius > width || 
mCenter.getmY() + mRadius > height ||
mCenter.getmX() - mRadius < 0 ||
mCenter.getmY() - mRadius < 0) {
return false; //not inside area
}
else { return true; }

在此代码中,mCenter 是一个具有 x 和 y 坐标的点,mRadius 是圆半径,宽度和高度是区域的宽度/高度

谢谢

最佳答案

您没有说出症状是什么,但是上面的有用图表使用普通的数学坐标系,而您发布的代码使用 awt.image.BufferedImage 。 Swing 和大多数 2D 计算机图形系统使用不同的坐标系,这样更方便按阅读顺序布局内容。

GraphicsConfiguration#getDefaultTransform() :

Coordinates in the coordinate space defined by the default AffineTransform for screen and printer devices have the origin in the upper left-hand corner of the target region of the device, with X coordinates increasing to the right and Y coordinates increasing downwards.

我认为可以设置 GraphicsConfiguration具有不同的变换。 (我不知道该怎么做。)awt.image.BufferedImage 则不然。 :

All BufferedImage objects have an upper left corner coordinate of (0, 0).

javax.swing.SwingUtilities 具有坐标转换方法。

附注调用image.setRGB()与将整个图像传递到 setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 相比,每个像素的速度会很慢或setData(Raster r) 。通常,帧缓冲区保存在一维数组中,该数组被视为二维数组,其中 scansize指示此缓冲区内扫描线的宽度。

关于java,判断圆是否在一个区域内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291881/

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