Solved

Error sorting vertices using a maximising heap

Posted on 2004-04-11
3
210 Views
Last Modified: 2013-11-23
Hi,
      I'm attempting to sort some 3D shapes by inserting their vertices into a max-heap and then removing the maximum value until the vertices are in descending order.  But so far i've encountered three errors. Please take a look at my code and the error messages and tell me what you think.

public class OcclusionPipeline extends Mesh implements RenderPipeline{
    Point3D[] pts;
    private int size;
    public int capacity(){return(pts.length);}
    public int size(){return(size);}
   
 
    public OcclusionPipeline(Point3D[] pts){
    super(pts);
   
    pts = new Point3D[2];
    size = 0;
       
}
   
   public OcclusionPipeline(int initialCapacity)
   {
         pts = new Point3D[initialCapacity];
         size = 0;
         }
         
   private int parent(int i)
      {
            return ((i - 1) / 2);
      }

      private int left(int i)
      {
            return (i * 2 + 1);
      }

      private int right(int i)
      {
            return (i * 2 + 2);
      }

      
      private void swap(int i, int j)
      {
            Point3D temp;

            temp   = pts[i];
            pts[i] = pts[j];
            pts[j] = temp;
      }

      
      private void heapify(int i)
      {
            int leftChild  = left(i);
            int rightChild = right(i);
            int greatest   = i;


            
            if(leftChild < size() && pts[leftChild] > pts[i])
                    greatest = leftChild;

            if (rightChild < size() && pts[rightChild] > pts[greatest])
                  greatest = rightChild;

            if (greatest != i) {
                  swap(i, greatest);
                  heapify(greatest);
            }
      }


  public void deleteMax()
      {
            
            if (size() > 0) {
                  pts[0] = pts[--size];
                  heapify(0);

                  if (size() * 2 < capacity() && size() > 2) {
                        Point3D[] tmp = new Point3D[size()];

                        for (int i = 0; i < tmp.length; i++)
                              tmp[i] = pts[i];

                        pts = tmp;
                  }
            }
            else {
                  System.err.println("deleteMax failed");
            }
      }

      
      public void insert(Point3D v_item)
      {
            int current;

            
            if (size == capacity()) {
                  Point3D[] tmp = new Point3D[2 * pts.length];

                  for (int i = 0; i < pts.length; i++)
                        tmp[i] = pts[i];

                  pts = tmp;
            }

            
            current = size++;

            
            while (current != 0 && pts[parent(current)] < v_item) {
                  
                  pts[current] = pts[parent(current)];

                  
                  current = parent(current);
            }

            
            pts[current] = v_item;
      }

      
      public Point3D max()
      {
            if (size > 0)
                  return pts[0];

            System.err.println("max failed");
            
      }

The javac compiler gives the following error messages:

Operator > cannot be applied to g3d.geom.Point3D, g3d.geom.Point3D
            if(leftChild < size() && pts[leftChild] > pts[i])
                                            ^

Operator > cannot be applied to g3d.geom.Point3D, g3d.geom.Point3D
            if (rightChild < size() && pts[rightChild] > pts[greatest])
                                                 ^

Operator < cannot be applied to g3d.geom.Point3D, g3d.geom.Point3D
            while (current != 0 && pts[parent(current)] < v_item)
0
Comment
Question by:capbry
  • 2
3 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
Comment Utility
ypou cannot use the < and > operators with your Point3D instances.
You need to instead compare the x, y, z values assciated with that object.
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 350 total points
Comment Utility
I'm assuming that Point3D is your own class? You must be having some getX (), getY (), getZ () methods (or some ways to access the co-ordinates)? Elaborating a little more on what objects said, you should use:

if ( rightChild.getX () < size () && .... so on
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Glad to help ;-)

BTW, why a C :-( you could've asked for more help if you wanted clarification.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now