Find biggest value

Blitz3D Forums/Blitz3D Programming/Find biggest value

Picklesworth(Posted 2004) [#1]
What would be the most sensible and simple way to find the furthest to the right and furthest to the left in a collection of vertices?
I have a bunch of Type instances, each containing the fields vert0, vert1, and vert2. I probably need to iterate through them in a For loop, and find the single vertice (of all vert0,vert1, and vert2) that is largest for x position and another that is smallest for x position. I think I have a way to do this, but it's confusing me which isn't a good sign, so you guys might have a magic way so I may as well ask. Please help.


Eikon(Posted 2004) [#2]
Wrong place for this, no need to double post this on BC either.


sswift(Posted 2004) [#3]
There is no magic way. Just iterate through them one at a time, and if you find one more to the left than the current leftmost one, then make that the leftmost one.


Kevin_(Posted 2004) [#4]
I would recommend using a sorting algorithm that uses an array to store the values. After sorting, you can then read the first and last element in the array to get the smallest and the biggest number.

There are quite a few sorting algorithms in the code archives. I recommend you stay clear of Bubble Sort though.


soja(Posted 2004) [#5]
No need to sort; that would take way to long for what you need. Just do what swifty said. You only have to iterate through them once; O(n).


Kevin_(Posted 2004) [#6]
Oh, I see now. Yes it would be quicker. Something like the code below?

[Code]
Graphics 640,480,32,2
SeedRnd(MilliSecs())

; Create a list of random numbers
Global N=10
Temp$=""
Dim List(N)
For V=1 To N
List(V)=Rand(0,255)
Temp$=Temp$+Str(List(V))+"," ; <-- Just for the report
Next

; Now find the smallest & largest number in the list
Biggest=Get_Biggest_Number()
Smallest=Get_Smallest_Number()

Text 10,10,"REPORT"
Text 10,40,"List = "+Temp$
Text 10,70,"Biggest Number = "+Str(Biggest)
Text 10,90,"Smallest Number = "+Str(Smallest)
Flip
WaitKey():End

; **********************************************

Function Get_Biggest_Number()
Biggest=List(1)
For V=2 To N
If List(V)>Biggest
Biggest=List(V)
EndIf
Next
Return Biggest
End Function

; **********************************************

Function Get_Smallest_Number()
Smallest=List(1)
For V=2 To N
If List(V)<Smallest
Smallest=List(V)
EndIf
Next
Return Smallest
End Function

; **********************************************

[/Code]


Kevin_(Posted 2004) [#7]
If you feel a bit adventurous you could just use one function to return both the lowest and highest values in the list.


Picklesworth(Posted 2004) [#8]
Thanks for the help, I think I've got it! Now I just have to work out my left from my right...
Something scary happened earlier, by the way. When I was typing tri0,tri1, and tri2, firstly I didn't put spaces as though it was a function, and secondly I though '1' in my head when I typed '0'. I think I'm going insane, lol.


Picklesworth(Posted 2004) [#9]
Oh yay I got it! I never thought that finding my left from my right was so bloody hard!

		For selected.selecteddata = Each selecteddata
		
			;DO THIS NOW! YOU KNOW WHAT IT IS DYLAN!
			;should be easy, you just have to choose the most suitable of the 3 vertex's and use it according to the sheet of note paper that is somewhere in here...
			
			If leftx# > VertexX(selected\surface,selected\vert0) Then LeftX# = VertexX(selected\surface,selected\vert0)
			If leftx# > VertexX(selected\surface,selected\vert1) Then LeftX# = VertexX(selected\surface,selected\vert1)
			If leftx# > VertexX(selected\surface,selected\vert2) Then LeftX# = VertexX(selected\surface,selected\vert2)
			If leftx# = 0.0
				If leftx# < VertexX(selected\surface,selected\vert0) Then LeftX# = VertexX(selected\surface,selected\vert0)
				If leftx# < VertexX(selected\surface,selected\vert1) Then LeftX# = VertexX(selected\surface,selected\vert1)
				If leftx# < VertexX(selected\surface,selected\vert2) Then LeftX# = VertexX(selected\surface,selected\vert2)
			EndIf
				
;too many if's... we're all going to die! A jusgement is upon us!!!		
			If rightx# < VertexX(selected\surface,selected\vert0) Then rightx# = VertexX(selected\surface,selected\vert0)
			If rightx# < VertexX(selected\surface,selected\vert1) Then rightx# = VertexX(selected\surface,selected\vert1)
			If rightx# < VertexX(selected\surface,selected\vert2) Then rightx# = VertexX(selected\surface,selected\vert2)
			If rightx# = 0.0
				If rightx# > VertexX(selected\surface,selected\vert0) Then rightx# = VertexX(selected\surface,selected\vert0)
				If rightx# > VertexX(selected\surface,selected\vert1) Then rightx# = VertexX(selected\surface,selected\vert1)
				If rightx# > VertexX(selected\surface,selected\vert2) Then rightx# = VertexX(selected\surface,selected\vert2)
			EndIf
			
		;	box\width = Rightx# - Leftx#
			
		Next
		

Yes, I know this could be better, but I also still have to do two other axis's and remaim sane while I put in my code that makes this worth-while.