「java判断矩形」java判断矩形是否在给定矩形范围内

博主:adminadmin 2023-03-21 06:14:09 414

本篇文章给大家谈谈java判断矩形,以及java判断矩形是否在给定矩形范围内对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

在java中判断一个点是否在一个有斜度的矩形内的 Rectangle的用法

只要实现这个的话,不考虑效率问题,可以new一个image,然后把这个不规则的四边fill上去,当然要指定个颜色,然后获取你要判断坐标的点。然后判断下颜色是不是和填充的一样

BufferedImage 有getRGB(int x, int y) 下面方法可以获取r、g、b的值

public static int [] getRGB(BufferedImage image, int x, int y) {

int [] rgb = null ;

if (image != null x image.getWidth() y image.getHeight()) {

rgb = new int [ 3 ];

int pixel = image.getRGB(x,y);

rgb[ 0 ] = (pixel 0xff0000 ) 16 ;

rgb[ 1 ] = (pixel 0xff00 ) 8 ;

rgb[ 2 ] = (pixel 0xff );

}

return rgb;

}

java中关于矩形类

public class Rectangle2 {

// 属性:

private double m_height;

private double m_width;

private double m_x1;

private double m_y1;

private double m_x2;

private double m_y2;

private boolean m_isRectangle;

// 操作:

public Rectangle2() {

this(0.0, 0.0, 0.0, 0.0);

}

public Rectangle2(double aX1, double aY1, double aX2, double aY2) {

setCoordinates(aX1, aY1, aX2, aY2);

}

public void setCoordinates(double aX1, double aY1, double aX2, double aY2) {

m_x1 = aX1;

m_y1 = aY1;

m_x2 = aX2;

m_y2 = aY2;

setHeight();

setWidth();

isRectangle();

}

public boolean isRectangle() {

if ((m_height = 0 m_width = 0) (m_x1 != m_x2 || m_y1 != m_y2)) {

System.err.println("输入的坐标构成一个矩形");

m_isRectangle = true;

} else {

if ((m_x1 == m_x2 m_y1 != m_y2)

|| (m_x1 != m_x2 m_y1 == m_y2)) {

System.err.println("输入的坐标构成一条直线");

} else if (m_x1 == m_x2 m_y1 == m_y2) {

System.err.println("输入的坐标构成一点");

}

m_isRectangle = false;

}

return m_isRectangle;

}

public boolean isSquare() {

return isRectangle() m_height == m_width;

}

private void setHeight() {

double side1 = Math.abs(m_x1 - m_x2);

double side2 = Math.abs(m_y1 - m_y2);

m_height = side1 = side2 ? side1 : side2;

}

private void setWidth() {

double side1 = Math.abs(m_x1 - m_x2);

double side2 = Math.abs(m_y1 - m_y2);

m_width = side1 = side2 ? side1 : side2;

}

public double getHeight() {

return m_height;

}

public double getWidth() {

return m_width;

}

public double perimeter() {

return 2 * (m_height + m_width);

}

public double area() {

return m_height * m_width;

}

}

public class Rectangle2Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Rectangle2 rect1 = new Rectangle2();

System.err.println(rect1.isRectangle());

System.err.println(rect1.isSquare());

System.err.println(rect1.getHeight());

System.err.println(rect1.getWidth());

System.err.println(rect1.perimeter());

System.err.println(rect1.area());

Rectangle2 rect2 = new Rectangle2(22, 33, 66, 99);

System.err.println(rect2.isRectangle());

System.err.println(rect2.isSquare());

System.err.println(rect2.getHeight());

System.err.println(rect2.getWidth());

System.err.println(rect2.perimeter());

System.err.println(rect2.area());

}

}

java中矩形类的问题

预判断,比如一点的坐标是:x,y,那么你可以先把他们得值传给x1,y1,然后移动x1,y1,如果碰撞了,则x,y的值不变,否则,把移动后的x1,y1赋值给x,y

用java编写一个判断两个矩形是否相交的函数怎么写?

你得到2个矩形的4个X坐标4个Y坐标 排序

得到 中间两个坐标的中值 得到个点坐标

2个矩形内都含有这点的话 两矩形相交

java知道四个点坐标,怎么判断一个点是不是在这个矩形区域内(矩形可能是斜着放的,有一定的斜度)

BOOL PtInRect(

CONST RECT *lprc, // address of structure with rectangle

POINT pt // structure with point

);

Parameters

lprc

Points to a RECT structure that contains the specified rectangle.

pt

Specifies a POINT structure that contains the specified point.

Return Values

If the specified point lies within the rectangle, the return value is nonzero.

该API在使用时的缺陷为不能够满足任意TRect,都能准确判断任意一点p是否在这个TRect上。

从p1点向p2点方向画矩形(两个点画矩形有4中可能的情况:即p1为左上,左下,右下,右上坐标点)

下面代码的编译开关请至于不同状态进行调试。下一篇文章会用另一个API解决该问题。

Delphi代码:

unit Unit2;

interface

{$DEFINE LEFTBOTTOM} //注释调此编译开关 PtInRect不能正确判断在该区域的点

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs;

type

TForm2 = class(TForm)

procedure FormCreate(Sender: TObject);

procedure FormPaint(Sender: TObject);

procedure FormMouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

private

{ Private declarations }

p1, p2: TPoint; //p1, p2构成矩形

FRect: TRect;

public

{ Public declarations }

end;

var

Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);

begin

//从p1点向p2点方向画矩形

{$IFDEF LEFTBOTTOM}

//p1为左下坐标,此时PtInRect能够正确判断在该区域的点

p1.X := 30;

p1.Y := 30;

p2.X := 200;

p2.Y := 110;

{$ELSE}

//p1为右上坐标,此时PtInRect不能正确判断在该区域的点

p1.X := 200;

p1.Y := 110;

p2.X := 30;

p2.Y := 30;

{$ENDIF}

FRect.Left := p1.X;

FRect.Right := p2.x;

FRect.Top := p1.y;

FRect.Bottom:= p2.y;

end;

procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

var

p: TPoint;

begin

p.X := x;

p.Y := y;

if PtInRect(FRect, p) then

ShowMessage('该点在矩形区域内');

end;

procedure TForm2.FormPaint(Sender: TObject);

begin

Canvas.Rectangle(FRect);

end;

end.

这是其他人写的东西,你看看行不行!我反正看不懂!

java判断矩形的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java判断矩形是否在给定矩形范围内、java判断矩形的信息别忘了在本站进行查找喔。