|
Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both!
Enjoy =)
if (pairCount>=28)
{
errBounce = FLOAT_MAX;
for (i1=0; i1<pairCount-14; i1++)
{
for (i2=i1+1; i2<pairCount-13; i2++)
{
for (i3=i2+1; i3<pairCount-12; i3++)
{
for (i4=i3+1; i4<pairCount-11; i4++)
{
for (i5=i4+1; i5<pairCount-10; i5++)
{
for (i6=i5+1; i6<pairCount-9; i6++)
{
for (i7=i6+1; i7<pairCount-8; i7++)
{
for (i8=i7+1; i8<pairCount-7; i8++)
{
for (i9=i8+1; i9<pairCount-6; i9++)
{
for (i10=i9+1; i10<pairCount-5; i10++)
{
for (i11=i10+1; i11<pairCount-4; i11++)
{
for (i12=i11+1; i12<pairCount-3; i12++)
{
for (i13=i12+1; i13<pairCount-2; i13++)
{
for (i14=i13+1; i14<pairCount-1; i14++)
{
for (i15=i14+1; i15<pairCount; i15++)
{
centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/15.0;
centroid[2] = (estBounce[i1][2] + estBounce[i2][2] + estBounce[i3][2] + estBounce[i4][2] + estBounce[i5][2] +
estBounce[i6][2] + estBounce[i7][2] + estBounce[i8][2] + estBounce[i9][2] + estBounce[i10][2] +
estBounce[i11][2] + estBounce[i12][2] + estBounce[i13][2] + estBounce[i14][2] + estBounce[i15][2])/15.0;
dis1 = PointDistance(centroid, estBounce[i1]);
dis2 = PointDistance(centroid, estBounce[i2]);
dis3 = PointDistance(centroid, estBounce[i3]);
dis4 = PointDistance(centroid, estBounce[i4]);
dis5 = PointDistance(centroid, estBounce[i5]);
dis6 = PointDistance(centroid, estBounce[i6]);
dis7 = PointDistance(centroid, estBounce[i7]);
dis8 = PointDistance(centroid, estBounce[i8]);
dis9 = PointDistance(centroid, estBounce[i9]);
dis10= PointDistance(centroid, estBounce[i10]);
dis11= PointDistance(centroid, estBounce[i11]);
dis12= PointDistance(centroid, estBounce[i12]);
dis13= PointDistance(centroid, estBounce[i13]);
dis14= PointDistance(centroid, estBounce[i14]);
dis15= PointDistance(centroid, estBounce[i15]);
dis = dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15;
if (dis<errBounce)
{
w1 = (dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w2 = (dis1+dis3+dis4+dis5+dis6+dis7+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w3 = (dis1+dis2+dis4+dis5+dis6+dis7+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w4 = (dis1+dis2+dis3+dis5+dis6+dis7+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w5 = (dis1+dis2+dis3+dis4+dis6+dis7+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w6 = (dis1+dis2+dis3+dis4+dis5+dis7+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w7 = (dis1+dis2+dis3+dis4+dis5+dis6+dis8+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w8 = (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis9+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w9 = (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis10+dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w10= (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9 +dis11+dis12+dis13+dis14+dis15)/(dis*14.0);
w11= (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9 +dis10+dis12+dis13+dis14+dis15)/(dis*14.0);
w12= (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9 +dis10+dis11+dis13+dis14+dis15)/(dis*14.0);
w13= (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9 +dis10+dis11+dis12+dis14+dis15)/(dis*14.0);
w14= (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9 +dis10+dis11+dis12+dis13+dis15)/(dis*14.0);
w15= (dis1+dis2+dis3+dis4+dis5+dis6+dis7+dis8+dis9 +dis10+dis11+dis12+dis13+dis14)/(dis*14.0);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
errBounce /= 15;
}
|
|
|
|
|
Geoff Bolton wrote: To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both
Granted I only glanced at the code for, like, 1 minute, but I haven't figured it either!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
I dont think that code will terminate
|
|
|
|
|
I'd like to see how this guy would create software to manage a global system. How many TiB would the source be?
|
|
|
|
|
The for() loops seem a rather verbose way of generating all 37,442,160 combinations of 15 pairs chosen from 28, but I wouldn't say they're necessarily horrible. If it is indeed necessary to enumerate all the combinations, nested 'for' loops seem a verbose but time-efficient way to do it. The w1..w15 calculations seem icky, though. Much better to compute the sum of dis1..dis15, and subtract one item from that computation.
|
|
|
|
|
Seems like a classic case for recursion to me...but I'm too tired to both trying to code it now...
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
Auch! It is terrible. I'm sure this can be reduced to a O(n^2) complexity with a smart algorithm and some precalculations/caching. If not, a simple recursive method can do it.
Greetings - Jacek
|
|
|
|
|
My God. It looks like some of the old stuff lying around at my company.
|
|
|
|
|
Below is an HTML file. Try to guess which DIV will be invisible in IE, then scroll down to see why...
<html>
<head>
<title>WTF</title>
<style type="text/css">
div
{
filter: alpha(opacity = 100);
border-color: Green;
border-style: solid;
border-width: 1px;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div style="background-color: #020509;"></div>
<div style="background-color: #02050A;"></div>
<div style="background-color: #02050B;"></div>
</body>
</html>
The middle DIV will be transparent because any element with the color #02050A that also has the filter set to an opacity (even if fully opaque) will appear transparent in IE8 (and probably other versions of IE).
|
|
|
|
|
Quite a feature.
This[^] seems to suggest one is in trouble as soon as some pixel in an image has that particular color!?
|
|
|
|
|
Quite right; that's actually one of the links I came across when Googling this issue. I am using jQuery to fade between images for a slideshow. Only problem is that some of the pixels would appear white during the fade transition. Turns out this IE bug is the reason why that's happening. The fix we decided on was to put a black image behind the images so that the transparent pixels will appear to be black. We could have also edited each image so the pixels weren't that specific color, but that's a bit hard to do when JPEG compression can alter colors... and I don't think the artists would appreciate having to edit a certain color out of all their images.
|
|
|
|
|
The real fix is to not use IE at all (not really an option, of course)
|
|
|
|
|
aspdotnetdev wrote: The fix we decided on was to put a black image behind the images so that the transparent pixels will appear to be black.
Could you use a background color that was 1 RGB value from from the transparent color? (e.g. #02050B instead of black) I think the difference wouldn't even be perceptible at that point.
Before .NET 4.0,
object Universe = NULL;
|
|
|
|
|
That's basically what we did, but we chose black, which is close enough to the color that the difference is imperceptible.
|
|
|
|
|
Oh, do you mean change the color of the ORIGINAL image, rather than the image behind it? As I explained in the post you replied to, that would be extra work for the designers and, due to JPEG compression, would be troublesome.
|
|
|
|
|
Hey folks, I don't generally post on these forums, but I do enjoy the CP newsletters. Anyways, I wanted to share a lovely snippet I wrote a little while ago. Maybe get some opinions? It's a bubble sort in C. It generates 20 random numbers from 0 to 99, and sorts them! Of course, it's fairly straightforward, so I'm sure you could guess that.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ARRAY_SIZE 20
#define PNT(s, l, a) fprintf(std##s, l, a)
#define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)
void print_array(int *array) {
int x = ARRAY_SIZE;
char* fmts[] = { "%d, ", "%d\n" };
--array;
LBL0:
if (PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0)
goto LBL0;
}
main() {
int iarray[ARRAY_SIZE];
int x, y;
int ofs = iarray - &ofs;
void (*prnt_func)(int*) = &print_array;
srand((unsigned int)time(NULL));
x ^= x;
LBL1:
if ((y = rand(), x < ARRAY_SIZE) && ((((x = x + 1) - 1)[iarray] = (y - (y / 100) * 100)) || 1))
goto LBL1;
PNT2LINES(out, Before sort, ---------------, \n);
(*prnt_func)(&ofs + ofs);
LBL2:
y = x &= ~x;
LBL3:
if (y + 1 < ARRAY_SIZE) {
if ((++y, iarray[y] < (y-1)[iarray]) && (y[iarray] ^= iarray[y-1] ^= iarray[y] ^= *(iarray + y - 1), x |= y));
goto LBL3;
}
if (x = x) goto LBL2;
PNT2LINES(out, After sort, ---------------, \n);
(*prnt_func)(&ofs + ofs);
}
|
|
|
|
|
Raise your hands, and step away from the keyboard, sir.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
I think my breakfast just revisited.
Why-oh-why-oh-why would you want to do that?
|
|
|
|
|
Yup. It's a horror.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
|
This was for an obfuscated C contest, right?
RIGHT?
(Must resist the urge to trace through it...)
He said, "Boy I'm just old and lonely,
But thank you for your concern,
Here's wishing you a Happy New Year."
I wished him one back in return.
|
|
|
|
|
nah, just felt like doing it
|
|
|
|
|
We're not taking our meds now, are we?
|
|
|
|
|
Wow... that's kind of a tour de force.
|
|
|
|
|
SirTimothy wrote: Anyways, I wanted to share a lovely snippet I wrote a little while ago.
How long is a "little while ago"?
|
|
|
|
|
mmm... last week? maybe the week before?
|
|
|
|
|