|
Post by bluatigro on Apr 27, 2018 6:36:26 GMT
this how you can do OOP in JB :
''bluatigro 27 apr 2018 ''complex OOP module
[complex] ''create complex object for output ''do gosub [complex] in begin code global complex.r , complex.i return sub complex r , i ''constructor complex object complex.r = r complex.i = i end sub ''operator's sub complex.add a1 , b1 , a2 , b2 call complex a1 + a2 , b1 + b2 end sub sub complex.sub a1 , b1 , a2 , b2 call complex a1 - a2 , b1 - b2 end sub sub complex.mul a1 , b1 , a2 , b2 call complex a1 * a2 - b1 * b2 _ , a1 * b2 + a2 * b1 end sub sub complex.div a1 , b1 , a2 , b2 q = a2 * a2 + b2 * b2 call complex ( a1 * a2 + b1 * b2 ) / q _ , ( a2 * b1 - a1 * b2 ) / q end sub ''function's function complex.length( a , b ) complex.length = sqr( a^2 + b^2 ) end function sub complex.sqr a1 , b1 q = length( a1 , b1 ) a = sqr( ( a1 + q ) / 2 ) b = 2 * a b = b1 / b call complex a , b end sub sub complex.ln a1 , b1 call complex log( length( a1 , b1 ) ) _ , atn( b1 / a1 ) end sub sub complex.exp a1 , b1 call complex exp( a1 ) * cos( b1 ) _ , exp( a1 ) * sin( b1 ) end sub
''bluatigro 27 apr 2018 ''v3d OOP math module
[v3d] ''create v3d object for output ''do gosub [v3d] in begin of code global v3d.x , v3d.y , v3d.z return
sub v3d x , y , z ''constructor v3d object v3d.x = x v3d.y = y v3d.z = z end sub ''operator's sub v3d.min a , b , c , d , e , f call v3d a - d , b - e , c - f end sub
sub v3d.add a , b , c , d , e , f call v3d a + d , b + e , c + f end sub
sub v3d.mul a , b , c , d call v3d a * d , b * d , c * d end sub
sub v3d.div a , b , c , d call v3d a / d , b / d , c / d end sub
''function's
sub v3d.cross x1 , y1 , z1 , x2 , y2 , z2 call v3d y2 * z1 - z2 * y1 _ , z2 * x1 - x2 * z1 _ , x2 * y1 - y2 * x1 end sub
sub v3d.normalize a , b , c l = v3d.lenght( a , b , c ) call v3d a / l , b / l , c / l end sub
function v3d.dot( a , b , c , d , e , f ) v3d.dot = a * d + b * e + c * f end function
function v3d.angle( a , b , c , d , e , f ) dt = v3d.dot( a , b , c , d , e , f ) la = v3d.lenght( a , b , c ) lb = v3d.lenght( d , e , f ) v3d.angle = acs( dt / ( la * lb ) ) end function
function v3d.lenght( a , b , c ) v3d.lenght = sqr( a * a + b * b + c * c ) end function
|
|
|
Post by B+ on Apr 27, 2018 15:02:53 GMT
The only OOP like aspect of the code above that I see is the variable.naming (with the dots), there are no objects, classes, inheritance... www.webopedia.com/TERM/O/object_oriented_programming_OOP.htmlI do see a lovely starter set of procedures for handling Complex numbers and important 3D calculations though I can't testify to their accuracy or completeness. I have a question: For the v3d.angle parameters are a, b the (x, y) coordinates of a point in 2D? and likewise the c, d set and the e, f set? And does it actually just convert 3 points into a 2D angle? Append: Oh they also might be polar coordinates? or vectors? I just finished updating a Battleship Game with AI and have been informed the next step in evolving this Game is to take it to the 3D level. Ha! I just wanted to make the AI smarter than it currently is but they were right, I've got to learn this 3D stuff sooner or later.
|
|
|
Post by tsh73 on Apr 27, 2018 17:11:17 GMT
|
|
|
Post by bluatigro on Apr 27, 2018 18:43:54 GMT
i remember that stuf i reviseted it i noticed that using string's for this is slower than what i have now posted
dynamic array's can not be done whitout string's
|
|
|
Post by tsh73 on Apr 27, 2018 18:47:00 GMT
Yes strings is slower But you can have many of objects that way.
Does your new method allows only for one object?
|
|
|
Post by bluatigro on Apr 27, 2018 19:05:44 GMT
i agree that it is not real OOP but it is OOP like the use of a constructor i got fromc++
in my last post i talked aboud a dynamic array so here it is whit a example
i remember a error in sort$() so be careful whit it it is a long time ago that i tested this
WindowWidth = DisplayWidth WindowHeight = DisplayHeight
nomainwin
global maxx , maxy , source , target , cut$ , o$ , c$ , nx , ny global false , true , pi , sorted , show
maxx = 10 maxy = 10 cut$ = "|" false = 0 true = not( false ) ' or ANY NON-ZERO value pi = atn( 1 ) * 4 sorted = false 'sort lookat-cels on distance target show = true 'show red seartch patern '' parent dim maze( maxx * maxy + 10 ) , p( maxx * maxy + 10 )
for y = 0 to 10 read q$ for x = 1 to len( q$ ) a =val( mid$( q$ , x , 1 ) ) if a = 2 then source = index( x , y ) if a = 3 then target = index( x , y ) maze( index( x , y ) ) = a next x next y
data "1111111111" data "1........1" data "1........1" data "1...111..1" data "1...1....1" data "1.2.1..3.1" data "1...1....1" data "1..11....1" data "1........1" data "1........1" data "1111111111"
open "A*" for graphics_nsb as #m
#m "trapclose [quit]" #m "goto 120 100 ; down ; fill cyan" for i = 1 to maxx for j = 0 to maxy call point i , j , maze( index( i , j ) ) next j next i
#m "flush"
ny = int( source / maxx ) nx = source - ny * maxx o$ = push$( o$ , str$( source ) , 0 ) ty = int( target / maxx ) tx = source - ty * maxx
while o$ <>"" and now <>target tel = 0 for q = 0 to 7 / 4 * pi step pi / 4 now = index( nx , ny ) lookat = index( nx + sign( sin( q ) ) , ny + sign( cos( q ) ) ) if maze( lookat ) <> 1 _ and not( element( c$ , str$( lookat ) ) ) _ and not( element( o$ , str$( lookat ) ) ) then if p( lookat ) = 0 then p( lookat ) = now mem( tel ) = lookat ly = int( lookat / maxx ) lx = lookat - ly * maxx af( tel ) = sqr( ( lx - tx ) ^ 2 + ( ly - ty ) ^ 2 )
if lookat = target then now = target '<< this line has no efect why ? tel = tel + 1 if show then t$ = time$() while t$ = time$() wend call point lx , ly , 5 end if if not( sorted ) then o$ = push$( o$, str$( lookat ) , false ) end if end if next q if sorted then for h = 0 to tel - 1 for l = 1 to h - 1 if af( h ) > af( l ) then q = af( h ) af( h ) = af( l ) af( l ) = q q = mem( h ) mem( h ) = mem( l ) mem( l ) = q end if next l next h if tel > 0 then for i = 0 to tel - 1 o$ = push$( o$ , str$( mem( i ) ) , false ) next i end if end if c$ = push$( c$ , str$( now ) , true ) o$ = pop$( o$ ) now = val( top$( o$ ) ) ny = int( now / maxx ) nx = now - ny * maxx
scan
wend
if o$ = "" then notice "No path found !!" else t =target while t <> source t = p( t ) ny = int( t / maxx ) nx = t -ny * maxx call point nx , ny , 4 wend end if
wait
sub point x , y , no select case no case 1 '#m "color darkred" #m "backcolor darkred" case 2 '#m "color blue" #m "backcolor blue" case 3 '#m "color green" #m "backcolor green" case 4 '#m "color yellow" #m "backcolor yellow" case 5 #m "backcolor red" case else '#m "color black" #m "backcolor darkgray" end select
#m "place " ; x * 40 + 100 ; " " ; y * 40 + 100
select case no case 4 #m "circlefilled 7" case 5 #m "circlefilled 10" case else #m "circlefilled 20" end select
end sub
[quit] close #m end
function sign( x ) sign = 0 if x < 0 then sign = -1 if x > 0 then sign = 1 end function
function index( x , y ) index = x + y * maxx end function
'' stack stuff end function bool$( x ) if x then uit$ = "TRUE" else uit$ = "FALSE" end if bool$ = uit$ end function
function push$( stack$ , object$ , l ) if object$ <> cut$ then if l <> 0 then push$ = object$ + cut$ + stack$ else push$ = stack$ + object$ + cut$ ''push$ = stack$ + cut$ + object$ end if else push$ = stack$ end if end function
function pop$( stack$ ) ''delete last object$ i = instr( stack$ , cut$ ) if stack$ = "" then pop$ = "" ''get right side of stack pop$ = mid$( stack$ , i + 1 , len( stack$ ) -i ) end function
function top$( stack$ ) ''read last object$ i = instr( stack$ , cut$ ) if stack$ = "" then top$ = "" else top$ = mid$( stack$ , 1 , i - 1 ) ' <<<<<<<< end if end function
function size( a$ ) result = 0 for i = 1 to len( a$ ) if mid$( a$ , i , 1 ) = cut$ then result = result + 1 next i size = result end function
function insertAt$( a$ , item$ , i ) p = 1 result$ = "" max = size( a$ ) if i < 0 then i = max + 2 - abs( i ) while p < i result$ = push$( result$ , word$( a$ , p , cut$ ) , false ) p = p + 1 wend result$ = push$( result$ , item$ , false ) while p <= max result$ = push$( result$ , word$( a$ , p , cut$ ) , false ) p = p + 1 wend insertAt$ = result$ end function
function insertSorted$( a$ , item$ , no , isStr ) p = 1 result$ = "" max = size( a$ ) done = false while p <= max and not( done ) m$ = word$( a$ , p , cut$ ) mp$ = word$( m$ , no ) ip$ = word$( item$ , no ) result$ = push$( result$ , m$ , false ) if isStr then if mp$ <= ip$ then result$ = push$( result$ , item$ , false ) done = true end if else if val( mp$ ) <= val( ip$ ) then result$ = push$( result$ , item$ , false ) done = true end if end if p = p + 1 wend while p < max m$ = word$( a$ , p , cut$ ) result$ = push$( result$ , m$ , false ) p = p + 1 wend insertSorted$ = result$ end function
function sort$( a$ , no , isStr ) ''this one has a error : my pc frezes max = size( a$ ) for i = 1 to max result$ = insertSorted$( result$ , word$( a$ , i , cut$ ) , no , isStr ) next i sort$ = result$ end function
function remove$( a$ , item$ ) p = 1 result$ = "" max = size( a$ ) while p <= max if word$( a$ , p , cut$ ) <> item$ then result$ = push$( result$ , word$( a$ , p , cut$ ) , false ) end if p = p + 1 wend remove$ = result$ end function
function removeAt$( a$ , i ) p = 1 result$ = "" max = size( a$ ) if i < 0 then i = max + 1 - abs( i ) while p <= max if p <> i then result$ = push$( result$ , word$( a$ , p , cut$ ) , false ) end if p = p + 1 wend removeAt$ = result$ end function
function reverse$( a$ ) ''python : a[ : : -1 ] p = 1 max = size( a$ ) while p <= max result$ = push$( result$ , word$( a$ , p , cut$ ) , true ) p = p + 1 wend reverse$ = result$ end function
function part$( a$ , b , e ) ''python : a[ b : e ] max = size( a$ ) if b < 1 then b = b + max if e < 1 then e = e + max for i = b to e r$ = push$( r$ , word$( a$ , i , cut$ ) , false ) next i part$ = r$ end function
function element( a$ , item$ ) p = instr( cut$ + a$ , cut$ + item$ + cut$ ) element = p > 0 end function
function suffle$( a$ ) max = size( a$ ) dice = int( rnd( 0 ) * max ) + 1 seed$ = word$( a$ , dice , cut$ ) for i = 2 to max dice = int( rnd( 0 ) * ( max - i ) ) + 1 p$ = word$( a$ , dice , cut$ ) uit$ = push$( uit$ , p$ , false ) a$ = removeAt$( a$ , dice ) next i suffle$ = uit$ end function
|
|
|
Post by bluatigro on Apr 27, 2018 19:11:01 GMT
yes you have only 1 object that is the output of the OOP sub's you can use that output object in the next OOP sub and so on
the a,b,c is 1 v3d and d,e,f the 2e i agree that is not good parameter naming shal i change that or is now clear ?
|
|
|
Post by B+ on Apr 27, 2018 23:25:53 GMT
yes you have only 1 object that is the output of the OOP sub's you can use that output object in the next OOP sub and so on the a,b,c is 1 v3d and d,e,f the 2e i agree that is not good parameter naming shal i change that or is now clear ? If a, b, c relates to a 3D point x1, y1, z1 and d, e, f relates to a second 3D point x2, y2, z2 then I got that part. Then the function returns an angle ? the 2 points with the system origin? (the angle from the origin?) or the the angle returned is the angle one point is from the other? Oh hey! are you guys talking about using string "arrays" (very long strings of data) as OOP objects? We were looking for an application for string "arrays" some time ago when I was working up all sorts of manipulations of string arrays like sorting or substitution or inserting or deleting or extending "arrays" because you could make subs and function that could handle that sort or "array" such that they would be acceptable arguments because they are just strings.
|
|
|
Post by tsh73 on Apr 28, 2018 5:21:54 GMT
Yes, kind of.
Not "array", because array is indexed list (1st 2nd 3rd etc) of the same-meaning values but "structure" where meanings of items are different (Name Position Age etc). List of pairs (name=value) would be close to it. (speaking of which there were several implementations on JB forums, I probably have mine somewhere). So sorting just don't apply.
Yes, that is main reason.
|
|
|
Post by bluatigro on Apr 28, 2018 10:01:46 GMT
the start of v3d a,b,c and v3d d,e,f is v3d 0,0,0
if you need a other point do v3d.angle(x1-x0,y1-x0,z1-z0,x2-x0,y2-y0,z2-z0)
angle is in radians
the string array can used as a python list but has not jet al python stuf in it
|
|
|
Post by B+ on Apr 28, 2018 15:38:10 GMT
Hi tsh73, With Word$() function, I consider all strings potential "arrays" element$ = Word$(StringArrayName$, index, [delimiter]) even without Word$ a string is an array of characters with element = mid$(source$, index, elementLength) '< more structured with fixed string lengths but you do need a way to reverse the function to insert, edit, or append new element values. Which I did work up at the old forum, so did Carl sort of... with a Python like dictionary, the name=value, stuff (which got me going BTW). But at the time, we couldn't come with an idea to use them... maybe an pseudo OOP attempt? ? I did sort them inside the strings with a quick sort method routine using pivots, glacial slow of course but is possible! (maybe wasn't in JB) Name=Value things would be easy to sort by Name or Value (I think). Hi Bluatigro, OK so the angle is with the origin of the system, got it. Thanks!
|
|
|
Post by bluatigro on Apr 28, 2018 15:58:44 GMT
b+ : for as i can remember sort$() did not work by mine code also it froze my pc
|
|
|
Post by B+ on Apr 28, 2018 20:11:33 GMT
Oh hey! Now I am seeing that bluatigro had word$ tools also in the 6th post of this thread. Sorry I missed that the first time around. I think that OOP word "Constructor" may of scared the mind from my brain and I boogied on out! Well the miss-take, got me to dig up my own word tools and post them, now it's fun to compare the two sets (for me anyway). bluatigro is using cut$ for what I call the delimiter but my tools use mostly default space. size function for my wCnt function insertAt insert are similar to my wSub or wPut subs Oh! his sort handles both strings and numbers, good idea! Well I think I've found another String "Array" fan! They are dynamic, so nice and flexible AND you can use them in JB sub and function calls because they are just strings. Heck I built an interpreter based on them after I made my JB EVAL function based on them at old forum.
|
|