@peceha, post #30
~1 KB?
@BULI, post #37
Wasze generatory liczb pseudolosowych, skąd pobierają "element peudolosowy"?W zasadzie tylko kod w poście #20 bierze entropię skądś, pozostałe zakładają, że ona już jest w wartości początkowej (seed) i nie obchodzi ich jak ją zorganizowaliśmy. Adres $DFF006 to rejestr VHPOSR, zawierajacy w starszym bajcie 8 młodszych bitów numeru aktualnie wyświetlanej linii obrazu, w młodszym bajcie poziomą pozycję w tej linii w pikselach podzieloną przez 2. Zawartość tego rejestru zmienia się co 280 ns. Adres $BFE801 należy do układu CIA A i jest to najmłodszy bajt licznika zliczającego impulsy synchronizacji pionowej obrazu. Adres $BFD800 z kolei to najmłodszy bajt licznika zliczającego impulsy synchronizacji poziomej.
@Krashan, post #19
@Hexmage960, post #41
@Hexmage960, post #41
@recedent, post #48
import bpy import math import random def create_mesh(verts): mesh = bpy.data.meshes.new("mesh") mesh.from_pydata(verts, [], []) mesh.update() return mesh def link_object(object): scene = bpy.context.scene scene.objects.link(object) def create_object(object_name, verts): object = bpy.data.objects.new(object_name, create_mesh(verts)) link_object(object) object.select = True verts = [] scale_factor = 100 # Rozmiar planszy (width, height) = (800, 800) # Wysokosc trojkata triangle_h = 600 triangle_a = int(triangle_h * 2 / math.sqrt(3)) # Koordynaty trojkata - zawsze na srodku okna triangle = ((width/2, height/2-triangle_h/2), (width/2 - triangle_a / 2, height / 2+triangle_h/2), (width/2 + triangle_a / 2, height/2+triangle_h/2)) # Wylosuj pierwszy punkt x = (random.randint(0, width-1), random.randint(0, height-1)) cnt = 0 running = True # Glowna petla while running: cnt = cnt + 1 # Rysuj punkt verts.append((x[0]/scale_factor, x[1]/scale_factor, 0)) # Wylosuj jeden z punktow trojkata tx = triangle[random.randint(0, 2)] # Oblicz nowe koordynaty x = ((x[0] + tx[0]) / 2, (x[1] + tx[1]) / 2) if cnt % 100000 == 0: running = False create_object("Trojkat Sierpinskiego", verts)
@MDW, post #46
Wszystko zależy jaki rozkład mają mieć liczby "losowe" i jaki zakres.
@teh_KaiN, post #6
MODULE 'intuition/intuition', 'intuition/screens', 'other/random' ENUM ERR_SCREEN, ERR_WINDOW; DEF wierzcholek, th, ta DEF win_width, win_height, win_pos_x, win_pos_y; DEF mywnd = NIL:PTR TO window, myscr = NIL:PTR TO screen, msg OBJECT punkt x:INT y:INT ENDOBJECT DEF punkty[3]:ARRAY OF punkt DEF pStart:punkt PROC main() HANDLE WriteF('Trojkat Sierpinskiego\n') seedRand() myscr:=LockPubScreen(NIL) IF myscr = NIL THEN Raise( ERR_SCREEN ) win_width:= myscr.width win_height:= myscr.height - myscr.barheight - 1 win_pos_y:= myscr.barheight + 1 win_pos_x:= 0 UnlockPubScreen(NIL,myscr) th:= win_height-4 ta:= ! 2.0 * th / 1.7320508 punkty[0].x := win_width/2 punkty[0].y := (win_height-th)/2 punkty[1].x := (win_width-ta)/2+ta punkty[1].y := (win_height-th)/2+th punkty[2].x := (win_width-ta)/2 punkty[2].y := (win_height-th)/2+th mywnd:=OpenW ( win_pos_x, win_pos_y, win_width, win_height , IDCMP_RAWKEY, WFLG_ACTIVATE OR WFLG_BORDERLESS, NIL, NIL, 1, NIL) IF mywnd = NIL THEN Raise( ERR_WINDOW ) pStart.x := getRandRange(win_width-1) pStart.y := getRandRange(win_height-1) REPEAT /* jedynym mozliwym messagem jest IDCMP_RAWKEY, wiec liczymy do pierwszego message'a */ msg:=GetMsg (mywnd.userport) wierzcholek := getRandRange(3) pStart.x := (pStart.x+punkty[wierzcholek].x)/2 pStart.y := (pStart.y+punkty[wierzcholek].y)/2 Plot(pStart.x,pStart.y, 1) UNTIL msg <> NIL CloseW( mywnd ) EXCEPT SELECT exception CASE ERR_SCREEN ; WriteF ('Error LockPubScreen()\n') CASE ERR_WINDOW ; WriteF ('Error OpenW()\n') ENDSELECT IF mywnd <> NIL THEN CloseW(mywnd) IF myscr <> NIL THEN UnlockPubScreen(NIL,myscr) ENDPROC
@retronav, post #55
@peceha, post #56
UWORD x = rand() % WINDOW_WIDTH;
UWORD x = (rand() * WINDOW_WIDTH) >> 16;
m68k-amigaos-gcc -noixemul -Os -m68020 -nostartfiles -fno-inline -o dywan main.c
#include <exec/types.h> #include <exec/execbase.h> #include <intuition/intuition.h> #include <intuition/screens.h> #include <workbench/startup.h> #include <graphics/gfxbase.h> #include <graphics/gfx.h> #include <dos/dos.h> #include <dos/dosextens.h> #include <proto/exec.h> #include <proto/graphics.h> #include <proto/intuition.h> #include <utility/tagitem.h> #undef SysBase #define SysBase (*(struct ExecBase **)4) int main(void); /* Minimalny kod startowy */ int _start(void) { struct Process *p = NULL; struct WBStartup *wbmsg = NULL; int ret; p = (struct Process *)SysBase->ThisTask; if (p->pr_CLI == 0) { WaitPort(&p->pr_MsgPort); wbmsg = (struct WBStartup *)GetMsg(&p->pr_MsgPort); } ret = main(); if (wbmsg) { Forbid(); ReplyMsg((struct Message *)wbmsg); } return ret; } #define WINDOW_WIDTH 400 #define WINDOW_HEIGHT 400 #define TRIANGLE_H (WINDOW_HEIGHT * 4 / 5) #define TRIANGLE_A (TRIANGLE_H * 2 * 1000 / 1732) ULONG seed = 1; UWORD rand(void) { seed = ((seed * 1103515245) + 12345) & 0x7fffffff; return seed; } struct Point { LONG x; LONG y; }; struct Point points[3] = { { WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 - TRIANGLE_H / 2 }, { WINDOW_WIDTH / 2 - TRIANGLE_A / 2, WINDOW_HEIGHT / 2 + TRIANGLE_H / 2 }, { WINDOW_WIDTH / 2 + TRIANGLE_A / 2, WINDOW_HEIGHT / 2 + TRIANGLE_H / 2 } }; struct TagItem myWindowTags[] = { { WA_InnerWidth, WINDOW_WIDTH }, { WA_InnerHeight, WINDOW_HEIGHT }, { WA_DepthGadget, TRUE }, { WA_CloseGadget, TRUE }, { WA_DragBar, TRUE }, { WA_Title, (Tag) "Dywan" }, { WA_Activate, TRUE }, { WA_GimmeZeroZero, TRUE }, { WA_IDCMP, IDCMP_MOUSEBUTTONS | IDCMP_CLOSEWINDOW }, { TAG_DONE, 0UL } }; int main(void) { struct IntuitionBase * IntuitionBase = NULL; struct GfxBase * GfxBase = NULL; IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0); if (IntuitionBase != NULL) { GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0); if (GfxBase != NULL) { struct Window *myWindow = OpenWindowTagList(0, myWindowTags); if (myWindow != NULL) { struct RastPort *rastPort = myWindow->RPort; BYTE running = TRUE; SetAPen(rastPort, 1); /* Wylosuj pierwszy punkt */ ULONG x = (rand() * WINDOW_WIDTH) >> 16; ULONG y = (rand() * WINDOW_HEIGHT) >> 16; Move(rastPort, points[0].x, points[0].y); Draw(rastPort, points[1].x, points[1].y); Draw(rastPort, points[2].x, points[2].y); Draw(rastPort, points[0].x, points[0].y); do { struct IntuiMessage *msg; while ((msg = (struct IntuiMessage *)GetMsg(myWindow->UserPort)) != 0) { if (msg->Class == IDCMP_CLOSEWINDOW) { running = FALSE; } ReplyMsg((struct Message *)msg); } /* Wylosuj punkt trojkata */ UWORD p = (rand() * 3) >> 16; x = (x + points[p].x) / 2; y = (y + points[p].y) / 2; WritePixel(rastPort, x, y); } while(running == TRUE); CloseWindow(myWindow); } CloseLibrary((struct Library *)GfxBase); } CloseLibrary((struct Library *)IntuitionBase); } return 0; }
@mschulz, post #57
if (IntuitionBase != NULL) { GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0); if (GfxBase != NULL) { struct Window *myWindow = OpenWindowTagList(0, myWindowTags); if (myWindow != NULL) {
Returning early, in this case, has several benefits: Obviously, there is no deep nesting like in the first version. This is enabled because we return early in case of errors: with a return (or throw) in the if block we do not need an else block for the rest of the function. In addition, handling the errors immediately enables us to push them out of our minds. We need not keep track of them to write that later else statement. Last but not least, the actual work is set at the end of the function and not hidden in the middle of that forest of if-else blocks. Arguably, that actual work should be factored out into its own function.