Moneo
|
 |
« Reply #75 on: March 28, 2006, 10:41:23 PM » |
|
Moneo:
Here's another one-liner to turn a switch on and off, in accordance with, and I quote you, "Most programmers consider TRUE as -1, and FALSE as 0."
x = -1 - x
Very good Ralph, I never saw that before. *****
|
|
|
Logged
|
|
|
|
Moneo
|
 |
« Reply #76 on: March 28, 2006, 10:45:35 PM » |
|
Did someone already post this one? A = NOT A Yes, LooseCaboose posted it back in the early days of the thread. But that's ok, it happens to be my favorite. Of course we're talking about flipping a switch that's 0 or -1. *****
|
|
|
Logged
|
|
|
|
na_th_an
|
 |
« Reply #77 on: March 29, 2006, 09:01:58 AM » |
|
In the spirit of the originally stated "to turn a switch on and off", would the very often used following line qualify? IF A = 1 THEN A = 0 ELSE A = 1 Yeah, that works Ralph, but we have been considering as algorithms that code which does not have any IF's. That is, strictly computational, either arithmetic or logical. ***** A = 1 - A Yeah, Nathan, that's very good. But we should always define that this algorithm will flip a switch which is either 0 or 1. BTW I forgot to mention this in the examples of the original rules of the thread. NOTE: Speaking of those examples, the following code is incorrect for flipping a 0 or 1 switch called sw: sw = sw xor sw It should be: sw = sw xor 1 I'm surprised nobody caught that. ***** Sure, that's what it does. Flip 0 to 1, 1 to 0... It was just an oneliner for the multiline IF version.
|
|
|
Logged
|
|
|
|
yetifoot
|
 |
« Reply #78 on: March 29, 2006, 01:57:21 PM » |
|
Heres one that CoderJeff posted over at freebasic.net in a discussion on working out the angle between two points. angle = Atan2(v.x - u.x, -(v.y - u.y)) * (180 / PI)
It returns a value from -180 To 180, so needs a small adjustment if you want to use 0-360, but i thought this was a great one liner. If angle < 0 Then angle += 360
|
|
|
Logged
|
EVEN MEN OF STEEL RUST. 
|
|
|
Anonymous
Guest
|
 |
« Reply #79 on: March 29, 2006, 02:09:10 PM » |
|
i saw this on fb,.net also. but, shouldnt it be angle += 360, regardless? because the way you have it now, -120, say, and 60, will return the same value.
angle = ( ( Atan2(v.x - u.x, -(v.y - u.y)) * (180 / PI) + 360 ) mod 360 )
not tested ;p
|
|
|
Logged
|
|
|
|
yetifoot
|
 |
« Reply #80 on: March 29, 2006, 02:19:19 PM » |
|
if you use mod then it reverts to integer, so i prefer the other way to keep the double precision. It only needs the +360 when it returns a value from -180 To 0 which is why i used the < 0 compare. Your version is a true one liner though which is cool.
|
|
|
Logged
|
EVEN MEN OF STEEL RUST. 
|
|
|
Anonymous
Guest
|
 |
« Reply #81 on: March 29, 2006, 02:24:43 PM » |
|
yeah... well coderJeff showed us both up with this one: angle = 180 - Atan2 ( v.x - u.x, v.y - u.y ) * 180 / PI haha, that guys a ninja edit upon testing, that didn't work! so i borrowed a bit from dr_d, that is, making radian transform a literal. here ya go: angle = 180 - ATan2 ( v.x - u.x, v.y - u.y ) * 57.29577951308232
|
|
|
Logged
|
|
|
|
yetifoot
|
 |
« Reply #82 on: March 29, 2006, 02:46:22 PM » |
|
angle = 180 - ATan2 ( v.x - u.x, v.y - u.y ) * 57.29577951308232 this one gives me the wrong answer when angle = 0, it returns -6.24.....e015 or something
|
|
|
Logged
|
EVEN MEN OF STEEL RUST. 
|
|
|
Anonymous
Guest
|
 |
« Reply #83 on: March 29, 2006, 02:52:39 PM » |
|
correct you are. but i mean thats like
.0000000000000006
...so its pretty close to zero, hahahah. all other angles work.
|
|
|
Logged
|
|
|
|
neuro
|
 |
« Reply #84 on: March 29, 2006, 04:08:42 PM » |
|
Here's my one-liner: Draw a circle, without using the CIRCLE command: SCREEN 12: h = 200: k = 200: r = 100: c = 13: FOR x = 0 TO INT(r / SQR(2)) + 1: y = SQR(r ^ 2 - x ^ 2): FOR a = -1 TO 1 STEP 2: FOR b = -1 TO 1 STEP 2: PSET (h + x * a, k + y * b), c: PSET (h + y * a, k + x * b), c: NEXT: NEXT: NEXT
draws a circle with center at (h, k), radius r and color c without using the CIRCLE command, or SIN or COS, and just two PSET commands. It may seems like a long line but it fits inside a QBASIC IDE line of code. ;) - neuro [edit]: I see one of the rules is, no compounded lines (with colons)... Here's another of my favorite algorithms: purpose: hang the computer uninterruptibly (written in C): for ( ; ; fork() ) ; or this one, which goes on and on 4ever... (or just uses up all stack space): int func(int x) return func(x+1);
|
|
|
Logged
|
ignatures suck
|
|
|
Antoni Gual
|
 |
« Reply #85 on: March 29, 2006, 04:13:30 PM » |
|
Old thread, I don't know if i have posted this before: Insert this in a line printing loop to print page by page if lcnt=maxlines then print "Press a key";: x$=input$(1):lcnt=0:cls else lcnt=lcnt+1
|
|
|
Logged
|
Antoni
|
|
|
Moneo
|
 |
« Reply #86 on: March 29, 2006, 05:26:31 PM » |
|
Old thread, I don't know if i have posted this before: Insert this in a line printing loop to print page by page if lcnt=maxlines then print "Press a key";: x$=input$(1):lcnt=0:cls else lcnt=lcnt+1
Nice bit of code, Antoni, but it's not one line of code by the established rules. *****
|
|
|
Logged
|
|
|
|
relsoft
|
 |
« Reply #87 on: March 29, 2006, 11:24:58 PM » |
|
x = y * (((z AND 1) = 1) OR 1) x = -y or + y depending on z being odd or even
|
|
|
Logged
|
|
|
|
Moneo
|
 |
« Reply #88 on: March 30, 2006, 08:52:15 PM » |
|
x = y * (((z AND 1) = 1) OR 1) x = -y or + y depending on z being odd or even Ok, but what would I use this algorithm for? *****
|
|
|
Logged
|
|
|
|
relsoft
|
 |
« Reply #89 on: March 31, 2006, 03:09:34 AM » |
|
GFX effects. :*)
Mostly plasmas and sinewave displacement.
|
|
|
Logged
|
|
|
|
|