简体   繁体   English

如何沿着Java形状行走并将其转换为左/右/直命令?

[英]How to walk along a Java shape and translate it to left/right/straight commands?

I have Java Shape objects. 我有Java Shape对象。 If I follow the line for example pixel by pixel it would translate to a sequence of straight, straight, ... commands. 如果我按照像素逐行跟踪,它将转换为一系列直线,直线......命令。 If I follow a circle, I would be something like left, straight, left, straight, ... How can I solve this with Java? 如果我按照一个圆圈,我会像左,直,左,直,...我怎么能用Java解决这个问题?

I would get a pathIterator from the shape, 我会从形状中得到一个pathIterator,

public PathIterator getPathIterator(AffineTransform at)

and from the iterator, call 并从迭代器中调用

int currentSegment(double[] coords) 

then given the starting and ending pt, you can calculate the slope of the line. 然后给出开始和结束点,你可以计算线的斜率。

At any segment along the way, if you have the slop of the previous line seqment, and the slope of the current line segment, you can tell in what relative direction to go using the dot product to determine the angle. 在沿途的任何路段,如果您有前一行序列的斜率和当前线段的斜率,您可以使用点积来确定相对方向以确定角度。

我可能会选择一个起始像素,将其存储在一个数组中,然后查找不在该数组中的周围像素,存储到达那里的方向,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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