- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在尝试实现四叉树来划分 map 。我过去一周做了研究,但没有成功。我试图将 map 分成各种矩形,根据人所在的位置,这些矩形将成为 map 的不同区域。我被困住了,因为出于某种原因,我的树无法超过 3 的深度。下面的代码是我的四叉树类。我附加了另外两个类,QuadNode 和 QuadRectangle。 QuadNode 是树中的节点,而 QuadRectangle 是玩家周围区域的矩形。
四叉树:
import java.util.*;
public class QuadTree {
private QuadNode root;
private QuadNode prevNode;
private double[][] points;
private double mapMinX;
private double mapMinY;
private double mapMaxX;
private double mapMaxY;
private int treeDepth;
private List<QuadRectangle> rectangles;
public void insert( double x, double y ){
QuadRectangle value = new QuadRectangle();
value.setPoint(x, y);
if( root == null ){
//System.out.println("Root");
QuadNode node = new QuadNode<>( value );
node.getRect().setLines( mapMinX, mapMinY, mapMaxX, mapMaxY );
root = node;
}
else {
//System.out.println("New Branch");
insertRec(root, value);
}
}
private void insertRec( QuadNode latest, QuadRectangle value ){
// Check to see if point is in NE -- x > x center AND y > y center
if ( latest.getRect().getCenterX() < value.getPointX() && latest.getRect().getCenterY() < value.getPointY() ){
if ( latest.getNE() == null ) {
QuadNode temp = new QuadNode<>(value);
temp.getRect().setLines(latest.getRect().getCenterX(), latest.getRect().getCenterY(), latest.getRect().getMaxX(), latest.getRect().getMaxY());
latest.setNE(temp);
}
else {
insertRec( latest.getNE(), value );
}
}
// Check to see if point is in NW -- x < x center AND y > y center
else if( latest.getRect().getCenterX() > value.getPointX() && latest.getRect().getCenterY() < value.getPointY() ) {
if ( latest.getNW() == null ){
QuadNode temp = new QuadNode<>(value);
temp.getRect().setLines(latest.getRect().getMinX(), latest.getRect().getCenterY(), latest.getRect().getCenterX(), latest.getRect().getMaxY());
latest.setNW(temp);
}
else {
insertRec( latest.getNW(), value );
}
}
// Check to see if point is in SE -- x > x center AND y < y center
else if( latest.getRect().getCenterX() < value.getPointX() && latest.getRect().getCenterY() > value.getPointY() ) {
if ( latest.getSE() == null ){
QuadNode temp = new QuadNode<>(value);
temp.getRect().setLines(latest.getRect().getCenterX(), latest.getRect().getMinY(), latest.getRect().getMaxX(), latest.getRect().getCenterY());
latest.setSE(temp);
}
else {
insertRec( latest.getSE(), value );
}
}
// Check to see if point is in SW -- x < x center AND y < y center
else if( latest.getRect().getCenterX() > value.getPointX() && latest.getRect().getCenterY() > value.getPointY() ) {
if ( latest.getSW() == null ){
QuadNode temp = new QuadNode<>(value);
temp.getRect().setLines(latest.getRect().getMinX(), latest.getRect().getMinY(), latest.getRect().getCenterX(), latest.getRect().getCenterY());
latest.setSW(temp);
}
else {
insertRec( latest.getSW(), value );
}
}
}
public QuadTree( double[][] vals, double minX, double minY, double maxX, double maxY ){
rectangles = new ArrayList<>();
points = vals.clone();
mapMinX = minX;
mapMinY = minY;
mapMaxX = maxX;
mapMaxY = maxY;
//orderPointsCounterClock();
for( int ii = 0; ii < vals[0].length; ii++){
insert(vals[0][ii], vals[1][ii]);
}
treeDepth = getDepth(root);
System.out.println(treeDepth);
addToList(root);
new DrawQuadTree( vals, rectangles ).setVisible(true);
}
private void orderPointsCounterClock(){
double originX = (mapMaxX + mapMinX) / 2;
double originY = (mapMaxY + mapMinY) / 2;
List<double[]> pointsDistance = new ArrayList<>();
boolean distFlag = true;
for( int ii = 0; ii < points[0].length; ii++){
double sqX = Math.pow( originX - points[0][ii], 2);
double sqY = Math.pow( originY - points[1][ii], 2);
double pointToOriginDist = Math.sqrt( sqX + sqY );
for(int jj = ii; jj < points[0].length; jj++){
double tempDist = Math.sqrt( Math.pow( originX - points[0][ii], 2) + Math.pow( originY - points[1][ii], 2) );
if( tempDist > pointToOriginDist ){
distFlag = false;
}
}
if( distFlag ){
pointsDistance.add( new double[]{ points[0][ii], points[1][ii] });
}
}
for( double[] l : pointsDistance ){
System.out.println( l[0] + " :: " + l[1] );
}
}
private int getDepth( QuadNode root ){
if( root == null ){
return 0;
}
else{
List maxValues = new ArrayList<>();
maxValues.add( 1 + getDepth( root.getNE() ));
maxValues.add( 1 + getDepth( root.getNW() ));
maxValues.add( 1 + getDepth( root.getSE() ));
maxValues.add( 1 + getDepth( root.getSW() ));
Collections.sort(maxValues);
return (int)maxValues.get( maxValues.size() - 1 );
}
}
public void addToList( QuadNode root ){
if( root != null ){
prevNode = root;
if( !rectangles.contains( prevNode.getRect() )) {
rectangles.add(prevNode.getRect());
}
addToList( root.getNE() );
addToList( root.getNW() );
addToList( root.getSE() );
addToList( root.getSW() );
}
else{
if( !rectangles.contains( prevNode.getRect() )) {
rectangles.add(prevNode.getRect());
}
}
}
}
四节点:
public class QuadNode<T> {
private QuadRectangle rect;
private QuadNode NE, NW, SE, SW;
public QuadNode( QuadRectangle value ){
this.rect = value;
this.NE = null;
this.NW = null;
this.SE = null;
this.SW = null;
}
public QuadRectangle getRect() {
return rect;
}
public void setNE(QuadNode NE) {
this.NE = NE;
}
public void setNW(QuadNode NW) {
this.NW = NW;
}
public void setSE(QuadNode SE) {
this.SE = SE;
}
public void setSW(QuadNode SW) {
this.SW = SW;
}
public QuadNode getNE() {
return NE;
}
public QuadNode getNW() {
return NW;
}
public QuadNode getSE() {
return SE;
}
public QuadNode getSW() {
return SW;
}
}
四边形:
public class QuadRectangle {
private Point[] line1;
private Point[] line2;
private Point[] line3;
private Point[] line4;
private double pointX;
private double pointY;
private double centerX;
private double centerY;
public void setOnlyPoint(boolean onlyPoint) {
this.onlyPoint = onlyPoint;
}
private double minX;
private double minY;
private double maxX;
private double maxY;
private boolean onlyPoint;
public QuadRectangle(){
line1 = new Point[2];
line2 = new Point[2];
line3 = new Point[2];
line4 = new Point[2];
}
public void setPoint( double x, double y ){
this.pointX = x;
this.pointY = y;
}
public void setLines( double minX, double minY, double maxX, double maxY ){
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
Point tr = new Point();
tr.setPoint( maxX, maxY );
Point tl = new Point();
tl.setPoint( minX, maxY );
Point bl = new Point();
bl.setPoint( minX, minY );
Point br = new Point();
br.setPoint( maxX, minY );
line1[0] = tl;
line1[1] = tr;
line2[0] = tr;
line2[1] = br;
line3[0] = br;
line3[1] = bl;
line4[0] = bl;
line4[1] = tl;
centerX = (maxX + minX)/2;
centerY = (maxY + minY)/2;
}
public boolean contains( double x, double y ){
Point point = new Point();
point.setPoint(x, y);
if( line1[0].xCord < point.xCord && line1[1].xCord > point.xCord ){
if( this.line2[0].yCord > point.yCord && this.line2[1].yCord < point.yCord ){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
public double getPointX(){
return this.pointX;
}
public double getPointY(){
return this.pointY;
}
public double getCenterX() {
return centerX;
}
public double getCenterY() {
return centerY;
}
public double getMinX() {
return minX;
}
public double getMinY() {
return minY;
}
public double getMaxX() {
return maxX;
}
public double getMaxY() {
return maxY;
}
public Point[] getLine1() {
return line1;
}
public Point[] getLine2() {
return line2;
}
public Point[] getLine3() {
return line3;
}
public Point[] getLine4() {
return line4;
}
对于这里的代码量,我深表歉意。我已经被困了几天了,我不知道该去哪里。
最佳答案
有很多实现。我刚刚创建了一个,你可以看看是否可以澄清你的想法:https://github.com/pvto/java-quadtree/blob/master/src/main/java/struct/quadtree/QuadTree.java .
这个想法是在 QuadNode 中进行所有比较。您可以完全删除 QuadRectangle 并将其替换为两对坐标:(x1,y1)(左上角)和(x2,y2)(右下角)。当您创建 QuadNode 时,您可以立即设置 x1,y1,x2,y2,并且之后不再更改它们。
您可以在该 QuadNode 中仅存储某种类型的一项,也可以像我一样存储一系列项目。如果它创建了子 QuadNode,那么它不应该存储任何项目,反之亦然。
希望这对您有所帮助。
关于java - 用于空间分区的四叉树 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31882829/
我想在我的 Tree 类中创建一个函数来遍历 n-ary Tree[T] 以取回具有 (level, T) 的元组,以便该 Tree 的用户可以执行类似 tree.traverse.foreach{
给定一个层次格式的数组,它们的直接子级存储在一个连续的数组中,返回一个 n 叉树 给定输入格式: [{'name':'a', 'level': -1}, {'name':'b', 'level
我要求教授给我一份另一个学期的旧作业。它是关于构建一个家谱,然后找到给定的两个节点之间的亲属关系。家谱是关于那美克星人(龙珠z)的,所以每个那美克星人都有一个父亲。 问题是输入是这样的: First
我正在尝试创建一个包含子 vector 的 n 叉树。 这就是我到目前为止所得到的。 在 node.h 文件中我有这个: #include #include using namespa
我正在尝试了解 n 叉树的预序遍历。我一直在阅读,我发现的所有示例都使用左子树和右子树,但是在 n 叉树中,什么是左子树,什么是右子树?有人可以给出一个很好的解释或伪代码吗? 最佳答案 而不是考虑 l
我应该反序列化一个 n 叉树。 这段代码创建了我的树: foodtree.addChildren("Food", { "Plant", "Animal" } ); foodtree.a
我正在尝试创建叉 TreeMap ,但仍然没有成功。这是我的代码: #include #include #include void procStatus(int level) { prin
我有一个二叉树,代表一个解析后的逻辑公式。例如,f = a & b & -c | d 由前缀表示法的列表列表表示,其中第一个元素是运算符(一元或二元),接下来的元素是它们的参数: f = [ |, [
我正在尝试根据给定的输入创建一棵树。那里将有一个根,包括子节点和子子节点。我可以实现树,在其中我可以将子节点添加到特定的主节点(我已经知道根)。但是,我试图弄清楚实现树的推荐方法是什么,我们必须首先从
我在 n 个节点上有一个完整的 19 元树。我标记所有具有以下属性的节点,即它们的所有非根祖先都是最年长或最小的 child (包括根)。我必须为标记节点的数量给出一个渐近界限。 我注意到 第一层有一
如何在不使用递归的情况下遍历 n 叉树? 递归方式: traverse(Node node) { if(node == null) return; for(Node c
我的树/节点类: import java.util.ArrayList; import java.util.List; public class Node { private T data;
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我在我的 Java 应用程序中有一个非 UI 使用的所谓的“k-ary”树,我想知道 javax.swing.tree 包是否是完成这项工作的正确工具,即使它与 Swing 打包在一起. 我有一类 W
我正在用 Java 实现 N 叉树;每个节点可以有尽可能多的节点。当我尝试 build 一棵树时,问题就来了。我有一个函数可以递归地创建一个特定高度的树,并根据节点列表分配子节点。当我调用该函数时,根
嗨,我有这段代码来搜索 n 叉树,但它不能正常工作,我不知道这有什么问题当搜索 n4 和 n5 时,它返回 n3怎么了? public FamilyNode findNodeByName(Family
哪个是 C 语言中 N 叉树的简洁实现? 特别是,我想实现一个 n 元树,而不是自平衡的,每个节点中的子节点数量不受限制,其中每个节点都包含一个已经定义的结构,例如: struct task {
#include #include #include typedef struct _Tree { struct _Tree *child; struct _Tree *
我正在编写文件系统层次结构的 N 叉树表示形式,其中每个节点都包含有关它所表示的文件/文件夹的一些信息。 public class TreeNode { private FileSystemE
如何在 R 中为给定数量的分支和深度构建 N 叉树,例如深度为 3 的二叉树? 编辑:将源问题与问答分开。 最佳答案 我想提出解决方案,我用它来构建树数据结构 叶安姆 分支因子。要将数据存储在树中,字
我是一名优秀的程序员,十分优秀!