Author Topic: Off center?  (Read 2635 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Off center?
« on: February 28, 2019, 10:41:01 am »
Code: QB64: [Select]
  1. DIM HandColor AS _UNSIGNED LONG
  2. HandColor = &HFF000000
  3.  
  4. SCREEN _NEWIMAGE(640, 640, 32)
  5. DEFLNG A-Z
  6.  
  7. Texture = _LOADIMAGE("wood.jpg", 32)
  8.  
  9. CircleFill 320, 320, 320, -1
  10.  
  11. FOR x = 0 TO 639: FOR y = 0 TO 639 'Paint the clock our wooden texture to make it pretty...
  12.         IF POINT(x, y) THEN _SOURCE Texture: PSET (x, y), POINT(x, y): _SOURCE 0
  13. NEXT y, x
  14.  
  15.  
  16. Minute = _NEWIMAGE(500, 80, 32)
  17. _DEST Minute
  18. FOR x = 250 TO 450
  19.     FOR y = 35 TO 45
  20.         PSET (x, y), HandColor
  21.     NEXT
  22. LINE (450, 35)-STEP(0, -5), HandColor
  23. LINE -STEP(50, 11), HandColor
  24. LINE -STEP(-50, 10), HandColor
  25. LINE -STEP(0, -10), HandColor
  26. PAINT (451, 35), HandColor
  27. CircleFill 250, 40, 20, HandColor
  28.  
  29.  
  30. Hour = ScaleImage(Minute, .75, 1)
  31. second = ScaleImage(Minute, 1, .25)
  32.  
  33. DisplayImage Minute, 320, 320, 90, 0
  34. DisplayImage Hour, 320, 320, 0, 0
  35. DisplayImage second, 320, 320, -90, 0
  36.  
  37.  
  38. SUB DisplayImage (Image AS LONG, x AS INTEGER, y AS INTEGER, angle AS SINGLE, mode AS _BYTE)
  39.     'Image is the image handle which we use to reference our image.
  40.     'x,y is the X/Y coordinates where we want the image to be at on the screen.
  41.     'angle is the angle which we wish to rotate the image.
  42.     'mode determines HOW we place the image at point X,Y.
  43.     'Mode 0 we center the image at point X,Y
  44.     'Mode 1 we place the Top Left corner of oour image at point X,Y
  45.     'Mode 2 is Bottom Left
  46.     'Mode 3 is Top Right
  47.     'Mode 4 is Bottom Right
  48.  
  49.  
  50.     DIM px(3) AS INTEGER, py(3) AS INTEGER, w AS INTEGER, h AS INTEGER
  51.     DIM sinr AS SINGLE, cosr AS SINGLE, i AS _BYTE
  52.     w = _WIDTH(Image): h = _HEIGHT(Image)
  53.     SELECT CASE mode
  54.         CASE 0 'center
  55.             px(0) = -w \ 2: py(0) = -h \ 2: px(3) = w \ 2: py(3) = -h \ 2
  56.             px(1) = -w \ 2: py(1) = h \ 2: px(2) = w \ 2: py(2) = h \ 2
  57.         CASE 1 'top left
  58.             px(0) = 0: py(0) = 0: px(3) = w: py(3) = 0
  59.             px(1) = 0: py(1) = h: px(2) = w: py(2) = h
  60.         CASE 2 'bottom left
  61.             px(0) = 0: py(0) = -h: px(3) = w: py(3) = -h
  62.             px(1) = 0: py(1) = 0: px(2) = w: py(2) = 0
  63.         CASE 3 'top right
  64.             px(0) = -w: py(0) = 0: px(3) = 0: py(3) = 0
  65.             px(1) = -w: py(1) = h: px(2) = 0: py(2) = h
  66.         CASE 4 'bottom right
  67.             px(0) = -w: py(0) = -h: px(3) = 0: py(3) = -h
  68.             px(1) = -w: py(1) = 0: px(2) = 0: py(2) = 0
  69.     END SELECT
  70.     sinr = SIN(angle / 57.2957795131): cosr = COS(angle / 57.2957795131)
  71.     FOR i = 0 TO 3
  72.         x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
  73.         px(i) = x2: py(i) = y2
  74.     NEXT
  75.     _MAPTRIANGLE (0, 0)-(0, h - 1)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  76.     _MAPTRIANGLE (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  77.  
  78. SUB CircleFill (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  79.     ' CX = center x coordinate
  80.     ' CY = center y coordinate
  81.     '  R = radius
  82.     '  C = fill color
  83.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  84.     DIM X AS INTEGER, Y AS INTEGER
  85.     Radius = ABS(R)
  86.     RadiusError = -Radius
  87.     X = Radius
  88.     Y = 0
  89.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  90.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  91.     WHILE X > Y
  92.         RadiusError = RadiusError + Y * 2 + 1
  93.         IF RadiusError >= 0 THEN
  94.             IF X <> Y + 1 THEN
  95.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  96.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  97.             END IF
  98.             X = X - 1
  99.             RadiusError = RadiusError - X * 2
  100.         END IF
  101.         Y = Y + 1
  102.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  103.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  104.     WEND
  105.  
  106. FUNCTION ScaleImage (Image AS LONG, xscale AS SINGLE, yscale AS SINGLE)
  107.     w = _WIDTH(Image): h = _HEIGHT(Image)
  108.     w2 = w * xscale: h2 = h * yscale
  109.     NewImage& = _NEWIMAGE(w2, h2, 32)
  110.     _PUTIMAGE (0, 0)-(w2 - 1, h2 - 1), Image&, NewImage&, (0, 0)-(w - 1, h - 1)
  111.     ScaleImage = NewImage&
  112.  

Since everybody has been playing around with fancy little clocks, I thought I'd have some fun and return to the classics -- a simple wood grained circle clock. 

For this, I decided to make my own simple little hands which I can rotate and spin to make all pretty, as in the program above.  It all looks fairly nice, with one minor exception -- the arrow at the end of the lines looks off center to me, as if it's slightly askew.  (I think the effect is very noticeable with the second hand as it's displayed for the current clock.)

The hands are made with a simple set of LINE commands, and should be centered properly to make a perfect little triangle, but they don't seem to do that to me.  It's probably something really simple off here, but I'm not seeing it, so I thought  I'd share and see if anybody notices the type/math error which I'm producing here. 

You'll need the wood.jpg texture from below to make the clock look pretty and proper.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Off center?
« Reply #1 on: February 28, 2019, 10:58:04 am »
What no rooster? Look you cock-a-doodle doofus, I've been holding back on every time you want to update a rooster clock, I can post, "Well hurry up, the cock is ticking!" But instead you give me wood. What am I supposed to do with that? It's hard sometimes. I mean when I ask for... oh wait, this isn't going to end well... rooster, I expect to get rooster!

It does have an old fashioned look to it, good job achieving that. I wonder if there is a way to apply that anti-alias trick to the triangles, that make up the points of the hands? If so, it would be polished off. Hey, polished off wood... I'm back!

Pete

 
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Off center?
« Reply #2 on: February 28, 2019, 11:19:26 am »
Hey Steve, try this:
Code: QB64: [Select]
  1. LINE (450, 35)-STEP(0, -15), HandColor
  2. LINE -STEP(50, 20), HandColor ' <<<<<<<<<<<<<<<<<<<< 10 not 11, oh make arrows points bigger for second hand
  3. LINE -STEP(-50, 20), HandColor
  4. LINE -STEP(0, -20), HandColor
  5. PAINT (451, 35), HandColor
  6. CircleFill 250, 40, 20, HandColor
  7.  
  8.  
  9.  

  [ You are not allowed to view this attachment ]