Go to bottom

Random line of code thread

category: code [glöplog]
Preacher: made my day
added on the 2014-06-12 09:06:01 by kbi kbi
Code: bool found = false; foreach(int index in comparisonLineIndicesArray[index0]) { if (index1==index) { found = true; break; } } if (!found) { foreach(int index in comparisonLineIndicesArray[index1]) { if (index0==index) { found = true; break; } } }

Code: bool found = comparisonLineIndicesArray[index0].Contains(index1) || comparisonLineIndicesArray[index1].Contains(index0);

I'm curious about the usage of this.
added on the 2014-06-12 11:53:17 by Orace Orace
@Orace: It was used to generate wireframe information from indexed polygon data.

Originally I had used a very slow bruteforce version where I was adding a polygon edge to a list and also comparing with every previous element in case this edge already existed (because it might be shared by more than one polygons).

Then I wrote this optimized version, having an array of lists, so if for example I want to search for edge with index points 7 and 15, I go to comparisonLineIndicesArray[7] and search if there is a second index with value 15 there. Also have to do comparisonLineIndicesArray[15] and search for 7, and if not then add the new edge to one of the lists. Much less time, almost instant instead of minutes for some of our big models.
added on the 2014-06-12 12:11:47 by Optimus Optimus
Oh, your second code is shorter version, I see :)
added on the 2014-06-12 12:13:48 by Optimus Optimus
You use a array of lists.
Does all the element of this array are used ?

I have good result with a Dictionary<int, HashSet<int>>.

Code: class EdgeContainer2 : IEdgeContainer { private readonly Dictionary<int, HashSet<int>> _comparisonLineIndicesArray = new Dictionary<int, HashSet<int>>(); /// <summary> /// Adds a edge to this container. /// </summary> /// <param name="edge">Edge to add.</param> /// <returns>true if edge id added to the container. false otherwise.</returns> public bool AddEdge(Edge edge) { var index0 = edge.Index0; var index1 = edge.Index1; var found = Found(index0, index1) || Found(index1, index0); if (found) { return false; } // The edge is not found, we add it. HashSet<int> set; _comparisonLineIndicesArray.TryGetValue(index0, out set); if (set == null) { // The set is not created yet, we create it. set = new HashSet<int>(); _comparisonLineIndicesArray.Add(index0, set); } // Add the edge. set.Add(index1); return true; } /// <summary> /// Return true if the oriented edge "index0-index1" is already in this container. /// </summary> private bool Found(int index0, int index1) { HashSet<int> set; _comparisonLineIndicesArray.TryGetValue(index0, out set); return (set != null) && set.Contains(index1); } public int Count { get { return _comparisonLineIndicesArray.Values.Sum(set => set.Count); } } }

I have a second implementation using SortedSet<Edge> (c# 4.0).
(For c# 3.0, you can use Dictionary<Edge, object> where you use only Keys and all value are null)

Where edge implement IComparable.
Code: public int CompareTo(object obj) { int result; if (obj == null) { result = 1; } else { var otherEdge = obj as Edge; if (otherEdge == null) { throw new ArgumentException("Object is not a Edge"); } result = otherEdge.Index0 - Index0; if (result == 0) { result = otherEdge.Index1 - Index1; } } return result; }

There is less code :

Code: class EdgeContainer1 : IEdgeContainer { private readonly SortedSet<Edge> _edgeList = new SortedSet<Edge>(); public bool AddEdge(Edge edge) { var result = false; lock (_edgeList) { result = _edgeList.Add(edge); } return result; } public int Count { get { return _edgeList.Count; } } }

But it three time slower :o(
added on the 2014-06-12 16:14:11 by Orace Orace
i wonder if Optimus is into C# at all! ;)

Oh, wait, random code here:
Code: // UNSIGNED !!!! float plane(float3 p) { return abs(p.y); } float sphere(float3 p,float x) { return length(p)-x; } float cube(float3 p,float3 x) { //return max(max(abs(p.x)-x.x,abs(p.y)-x.y),abs(p.z)-x.z); // many asciis //return length(max(abs(p)-x,0.)); // unsigned, artefacts p=abs(p)-x; // smallest in return max(max(p.x,p.y),p.z); // ascii-count -> 42 :D } // UNSIGNED !!!! float rcube(float3 p,float3 x,float y ) { return length(max(abs(p)-x,0.))-y; } // SIGNED - TEST float srcube(float3 p,float3 x,float y ) { //return max(max(abs(p.x)-x.x-y,abs(p.y)-x.y-y),abs(p.z)-x.z-y); p=abs(p)-(x-y); // smallest in return max(max(p.x,p.y),p.z); // ascii-count -> 42 :D } float ring(float3 p,float x,float y,float z) { //return max(abs(length(p.xz)-r)-r2,abs(p.y)-c); return max(abs(length(p.xy)-x)-y,abs(p.z)-z); } float octahedron(float3 p,float x) { p=abs(p); return (p.x+p.y+p.z-x)/3; } // TEST ONLY! float octahedron_extruded(float3 p,float x) { // p=abs(clamp(p,-1.,1.)); p=clamp(abs(p),0.,1.); return (p.x+p.y+p.z-x)/3; } float cylinderX(float3 p,float x,float y) { return max(abs(p.x)-y,length(p.yz)-x); } float cylinderY(float3 p,float x,float y) { return max(abs(p.y)-y,length(p.xz)-x); } float cylinderZ(float3 p,float x,float y) { return max(abs(p.z)-y,length(p.xy)-x); // return length(p.xy-y)-x; } float torusX(float3 p,float x,float y) { return length(float2(length(float2(p.y,p.z))-x,p.x))-y; } float torusY(float3 p,float x,float y) { return length(float2(length(float2(p.x,p.z))-x,p.y))-y; } float torusZ(float3 p,float x,float y) { return length(float2(length(float2(p.x,p.y))-x,p.z))-y; } float hexagonX(float3 p,float x,float y) { p=abs(p); return max(p.x-y,max(p.y+p.z*.5,p.z)-x); } float hexagonY(float3 p,float x,float y) { p=abs(p); return max(p.y-y,max(p.z+p.x*.5,p.x)-x); } float hexagonZ(float3 p,float x,float y) { p=abs(p); //return max(p.z-y,max(p.x+p.y*0.57735,p.y)-x); return max(p.z-y,max(p.x+p.y*.5,p.y)-x); } float octagonX(float3 p,float x,float y) { p=abs(p); return max(p.x-y,max(p.z+p.y*.5,p.y+p.z*.5)-x); } float octagonY(float3 p,float x,float y) { p=abs(p); return max(p.y-y,max(p.z+p.x*.5,p.x+p.z*.5)-x); } float octagonZ(float3 p,float x,float y) { p=abs(p); return max(p.z-y,max(p.y+p.x*.5,p.x+p.y*.5)-x); } float capsuleX(float3 p,float x,float y) { p=abs(p); return min(max(p.x-x,length(p.yz)-y),length(p-float3(x,0.,0.))-y); } float capsuleY(float3 p,float x,float y) { p=abs(p); return min(max(p.y-x,length(p.xz)-y),length(p-float3(0.,x,0.))-y); } float capsuleZ(float3 p,float x,float y) { p=abs(p); return min(max(p.z-x,length(p.xy)-y),length(p-float3(0.,0.,x))-y); } float prismX(float3 p,float x,float y) { return max(abs(p.z)-y,max(abs(p.y)*.9+p.x*.5,-p.x)-x*.5); } float prismY(float3 p,float x,float y) { return max(abs(p.z)-y,max(abs(p.x)*.9+p.y*.5,-p.y)-x*.5); } float prismZ(float3 p,float x,float y) { return max(abs(p.y)-y,max(abs(p.x)*.9+p.z*.5,-p.z)-x*.5); } // Strange Stuff: // y=.5 ?? float eightspheres(float3 p,float3 x,float y ) { return length(abs(p)-x)-y; } // y=.5 float wrongHexahedron(float3 p,float3 x,float y ) { p=abs(p)-x; p+=y*p.zxy; return max(max(p.x,p.y),p.z); } // x,y = .5 float wronglyOctagon(float3 p,float3 x,float y ) { p=abs(p)-x; p+=y*p.zyx; return max(max(p.x,p.y),p.z); }
Code: jsr demoflowupdate
added on the 2014-07-07 21:35:34 by Preacher Preacher
mov AX, 13h
added on the 2014-07-08 17:45:54 by movAX13h movAX13h
Code: idletime = idletime;
added on the 2014-08-28 08:52:37 by StingRay StingRay
A quote from some Pascal code that I really wrote years ago:
Code:if not working then break;

As soon as I finished typing that sentence, I had to grin :)
added on the 2014-08-28 09:17:37 by xTr1m xTr1m
Code:print(lambda f:f(f))(lambda s,l='',c='':lambda x=0:s(s,x,c+l)if x else c+l)('h')('e')('l')('l')('o')( ' ')('w')('o')('r')('l')('d' ;)()
added on the 2014-08-28 17:59:27 by linde linde
Code: eor.l #"KrIs",(a1)+
added on the 2014-09-09 11:26:04 by StingRay StingRay
Code:$('pouetbox_groupmain').select('.prod a').map(function(element){return element.readAttribute('href').split("=")[1]})

Code:$('.prod a').map(function(){return $(this).attr('href').split("=")[1]})
added on the 2014-09-09 23:23:02 by mog mog
Code:.loader bsr WaitRaster ; required for whatever reason
added on the 2014-09-18 10:56:25 by StingRay StingRay
lmpr: EQU &FA
hmpr: EQU &FB
vmpr: EQU &FC
added on the 2014-09-18 11:10:54 by Optimus Optimus
Code: fillScreensWithShit: LD D,15 loop2: LD A,D SLA A OUT (hmpr),A LD HL,&8000 LD BC,32768 loop1: ADD A,D ADD A,D ADD A,D ADD A,D LD (HL),A INC HL DEC BC LD A,B OR C JR NZ,loop1 DEC D JR NZ,loop2 RET
added on the 2014-09-18 11:11:37 by Optimus Optimus
(base+roid) * ambush * backstab * killing blow * [weapon bonus * (%deception + %iron hand + %counter strike + %rising shot + %be like water + %followthrouh + %like the wind + %COM + %BAR) + 0.RELIC] * execute * slag * crit * deathmark
added on the 2014-10-06 15:17:28 by Optimus Optimus
orace: setting AB=a+b for and edge, a and b indices, and comparing ABs is faster than comparing aa bb for each.
for example, to "smooth" a mesh, storing int value=K*x+MAX*(K*y+MAX*K*z) and comparing values before testing distance between two vertices. (K and MAX choosen optimaly)

I do not trust standard libs...
added on the 2014-10-06 18:12:21 by Barti Barti
Code:loop square1
From this intro of mine. :)

Code:; WARNING: explicit Random Seeds :) VOXOBJ_PRECALC GET CLOUDTEX,a1 FX_TEXGEN_CLOUD.P a1,#128,#"FUCK&q uot;,#"THIS"
added on the 2014-10-06 22:44:39 by StingRay StingRay
Code: illegal ; if this triggers, your bloody code is crap!
added on the 2014-10-20 10:37:39 by StingRay StingRay
description "FrontView"
position 0 0 1.1292
fieldOfView 0.2618 }
added on the 2014-10-20 12:30:41 by FunGas FunGas
Code:add.w #$242,a1 ; Tyranny (For You)
added on the 2014-11-24 09:45:44 by StingRay StingRay
Code: jsr -1012+(BOOTCODE-BOOT)(a2)
added on the 2014-11-28 21:25:49 by StingRay StingRay
Code:; one last thing: initialise the psg ld hl, #0000 ld de, #FFBF ld c, #FD ResetPSG: ld b, d out (c), l ld b, e out (c), h inc l bit 4, l ; gasman is too cool to have to do this :) jr z, ResetPSG
added on the 2014-11-29 11:04:54 by introspec introspec


Go to top