Solved

Error sorting vertices using a maximising heap

Posted on 2004-04-11
3
212 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
ID: 10803793
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:
Mayank S earned 350 total points
ID: 10803898
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:Mayank S
ID: 10839220
Glad to help ;-)

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

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

773 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