Home » Other » Training & Certification » help with new member function
help with new member function [message #288708] Tue, 18 December 2007 07:01 Go to next message
verd
Messages: 17
Registered: December 2007
Junior Member
hey guys, i'll try and explain this as best as i can, here goes...

Okay so basically what we’re trying to do here is compare two polygon sets. Remember that a polygon is a shape with many sides and corners. So for example one polygon can have four corners. So the co-ordinates for example maybe, 2, 2 3,5 5,5 and 5,6 whilst polygon two would be 2,3 3,5 6,5 and 3,2.
What we want to do is compare each corner of each polygon. In the instance that a corner does or doesn’t match, return 1 for true and 0 for false.
So far here is the header and body created for a corner and the polygon part follows. Below is a second member function in polygon to compare the corners, which i need to complete. I’ll write more about what i’ve accomplished in a few minutes.


create or replace type corner as object
( xval number,
yval number,
member function X return number,
member function Y return number,
member function CornerDistance(aCorner Corner ) return number
)
/
--create corner as body
create or replace type body corner as
member function X return number is
begin
return xval;
end;

member function Y return number is
begin
return yval;
end;

member function CornerDistance(aCorner Corner) return number is
Distance number;
begin
Distance := sqrt((self.x - aCorner.x)**2 + (self.y - aCorner.y)**2);
return Distance;
end;
end;
/
--varray for collection of corners
create or replace type CollectionOfCorners as varray(20) of Corner;
/

create or replace type polygon as object
(
listOfCorners CollectionOfCorners,
member function PerimeterOfPolygon return number,
member function compareCornerCollection return number
);
/

create or replace type body polygon as
member function PerimeterOfPolygon return number is
TotalOfPolygonPerimeter number;
begin
TotalOfPolygonPerimeter :=0;
for I in listOfCorners.first..listOfCorners.last
loop
if I < listOfCorners.last then
TotalOfPolygonPerimeter := TotalOfPolygonPerimeter + listOfCorners(I).CornerDistance(listOfCorners(listOfCorners.next(I)));
else
TotalOfPolygonPerimeter := TotalOfPolygonPerimeter + listOfCorners(I).CornerDistance(listOfCorners(listOfCorners.first));
end if;
end loop;
return TotalOfPolygonPerimeter;
end;


my question is i'm not too sure as to how to go about making the second member function for the polygon to compare the two polygons against each other.

there is a skeleton structure of that member function which i'll post later on.

if anyone can shed some sort of light on this matter, i'll really appreciate it!
Re: help with new member function [message #288715 is a reply to message #288708] Tue, 18 December 2007 07:21 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
this is what the skeleton looks like of the new member function. i havent run it yet because there are items missing.
member function compareCornerCollection(polygon1 polygon, polygon2 polygon)
return number is
BGIN
For i in polygon1.first..polygon2.last
For i in polygon2.first..polygon2.last
If polygon1(i)x == polygon2.x(i) then
Return 1 – polygon comparison is true
Else
Return 0 --polygon comparison is false
End if;
End;

can anyone please shed some light on this!?!?!
Re: help with new member function [message #288717 is a reply to message #288708] Tue, 18 December 2007 07:27 Go to previous messageGo to next message
Michel Cadot
Messages: 64140
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:

If polygon1(i)x == polygon2.x(i) then

This does not compile.
Can you at least post a code that compiles.
Read and follow OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format. Use the "Preview Message" button.
Also post your Oracle version (4 decimals).

And in the end, post a test case, an example of what you can have and what you want to execute and what you should get in this case.

Regards
Michel
Re: help with new member function [message #288719 is a reply to message #288708] Tue, 18 December 2007 07:31 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
its incomplete code, so it wont compile.
Re: help with new member function [message #288721 is a reply to message #288708] Tue, 18 December 2007 07:34 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
okay that statement has changed to something like this

if polygon1.x and polygon1.y == polygon2.x and polygon2.y then
return 1 ... etc

Re: help with new member function [message #288724 is a reply to message #288721] Tue, 18 December 2007 07:40 Go to previous messageGo to next message
Michel Cadot
Messages: 64140
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
This does not compile either.
What do you want from us, if this not PL/SQL question?

Regards
Michel
Re: help with new member function [message #288727 is a reply to message #288708] Tue, 18 December 2007 07:46 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
it is a pl/sql question. basically im not sure where to go from here :S could you shed some light on what i need to include further?
Re: help with new member function [message #288733 is a reply to message #288708] Tue, 18 December 2007 07:59 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
i am totally confused!
Re: help with new member function [message #288735 is a reply to message #288733] Tue, 18 December 2007 08:02 Go to previous messageGo to next message
Michel Cadot
Messages: 64140
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:

i am totally confused!

So am I.
I really don't understand what you want.

Please "post a test case, an example of what you can have and what you want to execute and what you should get in this case"

Regards
Michel
Re: help with new member function [message #288736 is a reply to message #288708] Tue, 18 December 2007 08:03 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
okay i will post as much as i can, later on. need to take a break before my head falls off! Neutral
Re: help with new member function [message #288750 is a reply to message #288735] Tue, 18 December 2007 10:08 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
Michel Cadot wrote on Tue, 18 December 2007 08:02

Quote:

i am totally confused!

So am I.
I really don't understand what you want.

Please "post a test case, an example of what you can have and what you want to execute and what you should get in this case"

Regards
Michel




okay, here goes...
this is what i have so far....
a polygon where i can calculate the perimeter from all the associated corners. this is split into two sections of code the first is the corner, the second is the polygon.
shown here in this code:
create or replace type corner as object
( xval number,
yval number,
member function X return number,
member function Y return number,
member function CornerDistance(aCorner Corner) return number
)
/
--create corner as body
create or replace type body corner as
member function X return number is
begin
return xval;
end;

member function Y return number is
begin
return yval;
end;

member function CornerDistance(aCorner Corner) return number is
Distance number;
begin
Distance := sqrt((self.x - aCorner.x)**2 + (self.y - aCorner.y)**2);
return Distance;
end;
end;
/

--here is an anonymouse block to test the code!
Declare
Corner1 corner := corner(10,10);
Corner2 corner := corner(20,20);
BEGIN
DBMS_OUTPUT.PUT_LINE('Distance between corners = '||
Corner1.CornerDistance(Corner2));
end;
/

okay so now we have the corners of the polygon and the distance calculated between each corner. now we need to calculated the perimeter of the whole polygon. this is done in the polygon object coding here:
--we need a varray because we have similar datatypes
create or replace type CollectionOfCorners as varray(20) of Corner;
/

--here is the polygon header and body
create or replace type polygon as object
(
listOfCorners CollectionOfCorners,
member function PerimeterOfPolygon return number
);
/

create or replace type body polygon as
member function PerimeterOfPolygon return number is
TotalOfPolygonPerimeter number;

begin

TotalOfPolygonPerimeter :=0;
for I in listOfCorners.first..listOfCorners.last
loop
if I < listOfCorners.last then
TotalOfPolygonPerimeter := TotalOfPolygonPerimeter + listOfCorners(I).CornerDistance(listOfCorners(listOfCorners.next(I)));
else
TotalOfPolygonPerimeter := TotalOfPolygonPerimeter + listOfCorners(I).CornerDistance(listOfCorners(listOfCorners.first));
end if;
end loop;
return TotalOfPolygonPerimeter;
end;
end;
/
--this is the anonymous block to test the polygon
declare
apolygon polygon := polygon(CollectionOfCorners(Corner(0,0), Corner(1,1),Corner(1,1)));
begin
dbms_output.put_line('the total perimeter is ' ||aPolygon.PerimeterOfPolygon());
end;
/


now my query is, how do i, compare two collections of corners!?

are you with me?
Re: help with new member function [message #288770 is a reply to message #288750] Tue, 18 December 2007 11:16 Go to previous messageGo to next message
Michel Cadot
Messages: 64140
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:

Read and follow OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format. Use the "Preview Message" button.
Also post your Oracle version (4 decimals).

Please again, follow this.
If you want we help, at least help us help you.

Regards
Michel
Re: help with new member function [message #288772 is a reply to message #288708] Tue, 18 December 2007 11:22 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
okay sorry this is the first time im using this forum. where is this orafaq form to read?
Re: help with new member function [message #288776 is a reply to message #288772] Tue, 18 December 2007 11:27 Go to previous messageGo to next message
Michel Cadot
Messages: 64140
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
It was in my first answer: OraFAQ Forum Guide
Use code tags.

Regards
Michel
Re: help with new member function [message #288778 is a reply to message #288708] Tue, 18 December 2007 11:29 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
sorry for being so thick, right im reading it!
Re: help with new member function [message #288782 is a reply to message #288708] Tue, 18 December 2007 11:42 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
oh i give up!
how do i get admins to delete my thread!?
Re: help with new member function [message #288790 is a reply to message #288782] Tue, 18 December 2007 12:57 Go to previous messageGo to next message
Michel Cadot
Messages: 64140
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Don't, maybe in a couple of days you will br able to post exactly what you need/want.

Regards
Michel
Re: help with new member function [message #288910 is a reply to message #288750] Wed, 19 December 2007 00:55 Go to previous messageGo to next message
Barbara Boehmer
Messages: 8636
Registered: November 2002
Location: California, USA
Senior Member
It is unclear what sort of output you want when you compare the polygons consisting of lists of corners. If you want the number of corners that match, then the following should do it. If you want something else, then you should be able to modify it to suit your needs.


  MEMBER FUNCTION compareCornerCollection (polygon2 polygon) RETURN NUMBER IS
    TotalMatchingCorners NUMBER;
  BEGIN
    TotalMatchingCorners := 0;
    FOR i IN SELF.listofcorners.FIRST .. SELF.listofcorners.LAST LOOP
      FOR j IN polygon2.listofcorners.FIRST .. polygon2.listofcorners.LAST LOOP
        IF SELF.listofcorners(i).x = polygon2.listofcorners(j).x 
          AND SELF.listofcorners(i).y = polygon2.listofcorners(j).y THEN
          TotalMatchingCorners := TotalMatchingCorners + 1;
        END IF;
      END LOOP;
    END LOOP;
    RETURN TotalMatchingCorners;
  END;




Here is a demonstration incorporating it into the rest of your code:

SCOTT@orcl_11g> drop type polygon
  2  /

Type dropped.

SCOTT@orcl_11g> drop type collectionofcorners
  2  /

Type dropped.

SCOTT@orcl_11g> drop type corner
  2  /

Type dropped.

SCOTT@orcl_11g> 
SCOTT@orcl_11g> create or replace type corner as object
  2  ( xval number,
  3  yval number,
  4  member function X return number,
  5  member function Y return number,
  6  member function CornerDistance(aCorner Corner) return number
  7  )
  8  /

Type created.

SCOTT@orcl_11g> create or replace type body corner as
  2  member function X return number is
  3  begin
  4  return xval;
  5  end;
  6  
  7  member function Y return number is
  8  begin
  9  return yval;
 10  end;
 11  
 12  member function CornerDistance(aCorner Corner) return number is
 13  Distance number;
 14  begin
 15  Distance := sqrt((self.x - aCorner.x)**2 + (self.y - aCorner.y)**2);
 16  return Distance;
 17  end;
 18  end;
 19  /

Type body created.

SCOTT@orcl_11g> 
SCOTT@orcl_11g> Declare
  2  Corner1 corner := corner(10,10);
  3  Corner2 corner := corner(20,20);
  4  BEGIN
  5  DBMS_OUTPUT.PUT_LINE('Distance between corners = '||
  6  Corner1.CornerDistance(Corner2));
  7  end;
  8  /
Distance between corners = 14.14213562373095048801688724209698078569

PL/SQL procedure successfully completed.

SCOTT@orcl_11g> 
SCOTT@orcl_11g> create or replace type CollectionOfCorners as varray(20) of Corner;
  2  /

Type created.

SCOTT@orcl_11g> 
SCOTT@orcl_11g> create or replace type polygon as object
  2  (
  3  listOfCorners CollectionOfCorners,
  4  member function PerimeterOfPolygon return number,
  5  MEMBER FUNCTION compareCornerCollection (polygon2 polygon) RETURN NUMBER
  6  );
  7  /

Type created.

SCOTT@orcl_11g> show errors
No errors.
SCOTT@orcl_11g> create or replace type body polygon as
  2  member function PerimeterOfPolygon return number is
  3  TotalOfPolygonPerimeter number;
  4  
  5  begin
  6  
  7  TotalOfPolygonPerimeter :=0;
  8  for I in listOfCorners.first..listOfCorners.last
  9  loop
 10  if I < listOfCorners.last then
 11  TotalOfPolygonPerimeter := TotalOfPolygonPerimeter + listOfCorners(I).CornerDistance(listOfCorners(listOfCorners.next(I)));
 12  else
 13  TotalOfPolygonPerimeter := TotalOfPolygonPerimeter + listOfCorners(I).CornerDistance(listOfCorners(listOfCorners.first));
 14  end if;
 15  end loop;
 16  return TotalOfPolygonPerimeter;
 17  end;
 18  
 19    MEMBER FUNCTION compareCornerCollection (polygon2 polygon) RETURN NUMBER IS
 20  	 TotalMatchingCorners NUMBER;
 21    BEGIN
 22  	 TotalMatchingCorners := 0;
 23  	 FOR i IN SELF.listofcorners.FIRST .. SELF.listofcorners.LAST LOOP
 24  	   FOR j IN polygon2.listofcorners.FIRST .. polygon2.listofcorners.LAST LOOP
 25  	     IF SELF.listofcorners(i).x = polygon2.listofcorners(j).x
 26  	       AND SELF.listofcorners(i).y = polygon2.listofcorners(j).y THEN
 27  	       TotalMatchingCorners := TotalMatchingCorners + 1;
 28  	     END IF;
 29  	   END LOOP;
 30  	 END LOOP;
 31  	 RETURN TotalMatchingCorners;
 32    END;
 33  
 34  end;
 35  /

Type body created.

SCOTT@orcl_11g> show errors
No errors.
SCOTT@orcl_11g> declare
  2  apolygon polygon := polygon(CollectionOfCorners(Corner(0,0), Corner(1,1),Corner(1,1)));
  3  begin
  4  dbms_output.put_line('the total perimeter is ' ||aPolygon.PerimeterOfPolygon());
  5  end;
  6  /
the total perimeter is 2.82842712474619009760337744841939615714

PL/SQL procedure successfully completed.

SCOTT@orcl_11g> DECLARE
  2    polygona polygon := polygon(CollectionOfCorners(Corner(2,2), Corner(3,5),Corner(5,5), Corner(5,6)));
  3    polygonb polygon := polygon(CollectionOfCorners(Corner(2,3), Corner(3,5),Corner(6,5), Corner(3,2)));
  4  BEGIN
  5    DBMS_OUTPUT.PUT_LINE ('total matching corners: ' || polygona.compareCornerCollection(polygonb));
  6  END;
  7  /
total matching corners: 1

PL/SQL procedure successfully completed.

SCOTT@orcl_11g> DECLARE
  2    polygona polygon := polygon(CollectionOfCorners(Corner(2,2), Corner(3,5),Corner(5,5), Corner(5,6)));
  3    polygonb polygon := polygon(CollectionOfCorners(Corner(2,2), Corner(3,5),Corner(6,5), Corner(3,2)));
  4  BEGIN
  5    DBMS_OUTPUT.PUT_LINE ('total matching corners: ' || polygona.compareCornerCollection(polygonb));
  6  END;
  7  /
total matching corners: 2

PL/SQL procedure successfully completed.

SCOTT@orcl_11g> 


Re: help with new member function [message #289056 is a reply to message #288910] Wed, 19 December 2007 06:38 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
Barbara Boehmer wrote on Wed, 19 December 2007 00:55

It is unclear what sort of output you want when you compare the polygons consisting of lists of corners. If you want the number of corners that match, then the following should do it. If you want something else, then you should be able to modify it to suit your needs.
...

wow, that response was overwhelming, thank you! i was reading till late last night on what to do etc. i shall be back later to put some sort of a response on here, to clarify things further. thank you ever so much for your reply barbara i really appreciate it!

[Edit MC: remove the whole Barbara post copy]

[Updated on: Wed, 19 December 2007 06:42] by Moderator

Report message to a moderator

Re: help with new member function [message #289097 is a reply to message #288708] Wed, 19 December 2007 12:43 Go to previous messageGo to next message
verd
Messages: 17
Registered: December 2007
Junior Member
hey guys, i've been working on this for a good few hours now. it's more clear to me what i have to do. i think i'll have to let my tutor know that i posted on here for help as i really dont want to be classified as a plagiarist.

anyway i may be back later to show you what i've done!
Re: help with new member function [message #289120 is a reply to message #289097] Wed, 19 December 2007 14:30 Go to previous messageGo to next message
Barbara Boehmer
Messages: 8636
Registered: November 2002
Location: California, USA
Senior Member
verd wrote on Wed, 19 December 2007 10:43

i think i'll have to let my tutor know that i posted on here for help as i really dont want to be classified as a plagiarist.



Oops, I didn't realize this was homework or I would have given you hints and links to documentation and such, instead of complete code. I will move this from newbies to homework. In the future, please post homework in the homework forum.

Re: help with new member function [message #289292 is a reply to message #289120] Thu, 20 December 2007 10:20 Go to previous message
verd
Messages: 17
Registered: December 2007
Junior Member
Barbara Boehmer wrote on Wed, 19 December 2007 14:30

verd wrote on Wed, 19 December 2007 10:43

i think i'll have to let my tutor know that i posted on here for help as i really dont want to be classified as a plagiarist.



Oops, I didn't realize this was homework or I would have given you hints and links to documentation and such, instead of complete code. I will move this from newbies to homework. In the future, please post homework in the homework forum.




Thanks Barbara! Well, I'm a newbie here and didn't know which board to post in! I didn't even see the other board!!!! Also, I haven't copied your code word for word. AND I've been reading up on IF statements! Plus I have a better sense of direction for the second part of the assignment!

Anyway, your coding really helped me out. I've managed to make sense of my code on paper, but I need to run it to see what happens. God I felt so lazy today, really didn't feel like doing anything at all. I shall be posting here soon, to let you guys know how I get along!

Many thanks once again!
Previous Topic: Use of CASE expression in Join
Next Topic: creating new database
Goto Forum:
  


Current Time: Fri Dec 09 03:57:02 CST 2016

Total time taken to generate the page: 0.05805 seconds