图形就必然会存在几何的数学问题:点的旋转,求交点。
1、求点A(a,b)旋转β弧度后B的坐标:
/**
* Returns a new transformed Point of the input Point based on the
* transformation values set.
*/
public Point getTransformed(Point p) {
double x = p.x;
double y = p.y;
double temp;
x *= scaleX;
y *= scaleY;
temp = x * cos - y * sin;
y = x * sin + y * cos;
x = temp;
return new Point(Math.round(x + dx), Math.round(y + dy));
}
a = cosα
b = sinα
x = cos(α + β) = cosα*cosβ - sinα*sinβ
y = sin(α + β) = sinα*cosβ + cosα*sinβ
=>
x = acosβ - bsinβ
y = bcosβ + asinβ
2、计算交点
(1)长方形外一点P以及长方形中心O,求PO的连线与长方形的交点:
/**
* Gets a Rectangle from {@link #getBox()} and returns the Point where a
* line from the center of the Rectangle to the Point <i>reference</i>
* intersects the Rectangle.
*/
public Point getLocation(Point reference) {
Rectangle r = Rectangle.SINGLETON;
r.setBounds(getBox());
r.translate(-1, -1);
r.resize(1, 1);
getOwner().translateToAbsolute(r);
float centerX = r.x + 0.5f * r.width;
float centerY = r.y + 0.5f * r.height;
if (r.isEmpty() || (reference.x == (int) centerX && reference.y == (int) centerY))
return new Point((int) centerX, (int) centerY); // This avoids divide-by-zero
float dx = reference.x - centerX;
float dy = reference.y - centerY;
// r.width, r.height, dx, and dy are guaranteed to be non-zero.
float scale = 0.5f / Math.max(Math.abs(dx) / r.width, Math.abs(dy) / r.height);
dx *= scale;
dy *= scale;
centerX += dx;
centerY += dy;
return new Point(Math.round(centerX), Math.round(centerY));
}
1> 判断象限(dx,dy的符号可以确定)
2> 与那条边相交:
float scale = 0.5f / Math.max(Math.abs(dx) / r.width, Math.abs(dy) / r.height);
根据比例算出大边!然后通过相似三角形进行计算。
(2) 椭圆外一点P以及椭圆中心O,求PO的连线与椭圆的交点:
/**
* Returns a point on the ellipse (defined by the owner's bounding box)
* where the connection should be anchored.
*/
public Point getLocation(Point reference) {
Rectangle r = Rectangle.SINGLETON;
r.setBounds(getOwner().getBounds());
r.translate(-1, -1);
r.resize(1, 1);
getOwner().translateToAbsolute(r);
// 相当于
// ref.x = reference.x - r.getCenter().x;
// ref.y = reference.y - r.getCenter().y;
Point ref = r.getCenter().negate().translate(reference);
if (ref.x == 0)
return new Point(reference.x, (ref.y > 0) ? r.bottom() : r.y);
if (ref.y == 0)
return new Point((ref.x > 0) ? r.right() : r.x, reference.y);
float dx = (ref.x > 0) ? 0.5f : -0.5f;
float dy = (ref.y > 0) ? 0.5f : -0.5f;
// ref.x, ref.y, r.width, r.height != 0 => safe to proceed
float k = (float) (ref.y * r.width) / (ref.x * r.height);
k = k * k;
return r.getCenter().translate((int) (r.width * dx / Math.sqrt(1 + k)),
(int) (r.height * dy / Math.sqrt(1 + 1 / k)));
}
1>计算角度θ;
2>通过椭圆的三角坐标代换算出坐标值。
- 大小: 16.5 KB
- 大小: 11.5 KB
- 大小: 20.3 KB
分享到:
相关推荐
eclipse draw2d实例大全 org.eclipse.draw2d.examples.cg org.eclipse.draw2d.examples.connections org.eclipse.draw2d.examples.graph org.eclipse.draw2d.examples.hittest org.eclipse.draw2d.examples.images ...
GEF Draw2D 使用例子,可以直接运行的
Draw2d js图形库,画各种流程图,非常强大的javascript类图库。
如果您想以图形形式描绘将展示的数据,那么Draw2D是一个好工具。可以使用Draw2D编写自己的用来绘制图形的Java代码,这有助于您将精力集中于缩放代码和绘制代码上,把其他与绘制相关的工作留给Draw2D和SWT。您还可以...
draw2d 绘制图形教程 draw2d 绘制图形教程draw2d 绘制图形教程 draw2d 绘制图形教程
Draw2d API Draw2d是一个宿主在SWT Composite控件中的轻量级的构件(widge)系统。一个Draw2d应用程序由一个 SWT Composite控件, 一个轻量级系统, 以及其内容(figures)组成。
eclipse Draw2d Programmer Guide
Draw2D:eclipse Draw2D documents and samples.
网上的Draw2D的参考资料实在是太少了,对于新手来说太不友好了,所以,我总结了一份只有10来页的PPT做为新手的快速入门教程。
GEF/Draw2D入门例子 主要是Draw2d的几个入门的例子和入门的书籍 对eclipse中图形的绘画讲解
Draw2d教程,网上的,我收集了一下。 写的很详细
org.eclipse.draw2d_3.1.0.jarorg.eclipse.draw2d_3.1.0.jarorg.eclipse.draw2d_3.1.0.jarorg.eclipse.draw2d_3.1.0.jar
Draw2D教程,介绍了Draw2D的基本知识,对学习Draw2D有所帮助
swt总结draw2d绘图
使用Draw2d做的流程图工具
draw2d_Demo_code
eclipse draw2d 示例代码 码很全的。。。。
用Draw2d绘制曲线图形,在eclipse环境下运行
这是一个Draw2d的开发文档,可以学习一下eclipse插件开发的一些画图技术
基于draw2d和easyui开发的一个流程管理原型界面,能拖拽,根据拖拽的内容生成xml内容,根据xml自动解析生成界面