Solved

Error sorting vertices using a maximising heap

Posted on 2004-04-11
3
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

627 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