Draw a pixel: Difference between revisions

Added Uiua solution
(→‎{{header|Commodore BASIC}}: Add other graphical BASICs)
(Added Uiua solution)
 
(29 intermediate revisions by 22 users not shown)
Line 10:
=={{header|Action!}}==
The solution of this task is tricky because of technical limitation of Atari 8-bit computer. The computer supports high resolution graphics mode of 320 pixels width, 192 pixels height and 1 bit per pixel color. The color of the pixel cannot be set independently from the background color and only different luminance can be specified. To draw a red pixel the background must be set to light-red color.
<langsyntaxhighlight Actionlang="action!">PROC Main()
BYTE
CH=$02FC, ;Internal hardware value for last key pressed
Line 28:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Draw_a_pixel.png Screenshot from Atari 8-bit computer]
Line 34:
=={{header|Ada}}==
{{libheader|SDLAda}}
<langsyntaxhighlight Adalang="ada">with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Events.Events;
Line 80:
Window.Finalize;
SDL.Finalise;
end Draw_A_Pixel;</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 620:
pop {r4, lr}
bx lr
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
From [https://www.autohotkey.com/boards/viewtopic.php?p=64817#p64817 AHK Forum]
<langsyntaxhighlight AutoHotkeylang="autohotkey">Gui, Add, Picture, x100 y100 w2 h2 +0x4E +HWNDhPicture
CreatePixel("FF0000", hPicture)
Gui, Show, w320 h240, Example
Line 635:
DllCall("Gdi32.dll\SetBitmapBits", "Ptr", hBM, "UInt", 3, "Ptr", &BMBITS)
DllCall("User32.dll\SendMessage", "Ptr", Handle, "UInt", 0x172, "Ptr", 0, "Ptr", hBM)
}</langsyntaxhighlight>
 
=={{header|BASIC256BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="basic"> 100 WIDTH = 320
110 HEIGHT = 240
120 C = 1: REM RED
130 X = 100
140 Y = 100
150 DEF FN X(X) = INT ((X - 1) / 14)
160 DEF FN Y(Y) = INT ((Y - 1) / 8)
170 W = FN X(WIDTH)
180 H = FN Y(HEIGHT)
190 WX = INT ( RND (1) * (40 - W))
200 WY = INT ( RND (1) * (48 - H))
210 C$ = "0123456789:;<=>?"
220 C$ = MID$ (C$,C,I) + MID$ (C$,C + 2, LEN (C$) - C - 1)
230 I = INT ( RND (1) * LEN (C$))
240 COLOR= ASC ( MID$ (C$,I + 1)) - 48
250 C$ = MID$ (C$,1,I) + MID$ (C$,I + 2, LEN (C$) - I - 1)
260 A = PEEK (49234) + PEEK (49240) + PEEK (49232)
270 FOR I = 0 TO 39
280 VLIN 0,47 AT I
290 NEXT
300 COLOR= ASC ( MID$ (C$, INT ( RND (1) * LEN (C$)) + 1)) - 48
310 FOR I = WX TO WX + W
320 VLIN WY,WY + H AT I
330 NEXT
340 COLOR= C
350 PLOT WX + FN X(X),WY + FN Y(Y)
360 WAIT 49152,128
370 TEXT
380 HOME</syntaxhighlight>
 
==={{header|BASIC256}}===
It seems that the program should be this. And the BASIC256 tutorial programs work on my ubuntu system. This program neither draws the pixel nor resizes the window. Can't see anything when I plot many spots. Oh well. I've tried the rgb(255,0,0) function as well as the fastgraphics/refresh statements.
<syntaxhighlight lang="basic256">
<lang BASIC256>
rem http://rosettacode.org/wiki/Draw_a_pixel
 
Line 645 ⟶ 677:
color red
plot 100, 100
</syntaxhighlight>
</lang>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> VDU 23, 22, 320; 240; 8, 8, 8, 0128, 18, 0, 19, 25, 69, 100; 100;</langsyntaxhighlight>
 
==={{header|CCommodore BASIC}}===
The Commodore 8-bit machines only had 200 lines of vertical resolution (and the VIC-20 rarely used more than 160 of them for bitmap graphics), so these examples do not quite fit the task's 240-line requirement.
Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<lang C>
#include<graphics.h>
 
int main()
{
initwindow(320,240,"Red Pixel");
putpixel(100,100,RED);
getch();
return 0;
}
</lang>
 
=={{header|Commodore BASIC}}==
The Commodore 8-bit machines only had 200 lines of vertical resolution (and the VIC-20 rarely used more than 160 of them), so these examples do not quite fit the task's 240-line requirement.
 
'''Example 1:''' VIC-20 with SuperExpander Cartridge
Line 675 ⟶ 690:
Version 2 of Commodore BASIC, which shipped on the VIC-20 and Commodore 64, did not have any graphics support. A stock VIC didn't have enough memory for high-resolution graphics anyway, so they mostly weren't used. But Commodore shipped a "SuperExpander" cartridge for the VIC which not only included enough extra RAM to support a 160x160-pixel bitmap, but also extended BASIC to include statements for drawing on it. One oddity about the VIC's iteration of the SuperExpander is that, despite the low 160x160 resolution, it treats the screen as having 1024x1024 pixels; the below program compensates for this by asking for a dot at 640,640, which maps to the physical pixel at 100,100.
 
<langsyntaxhighlight basiclang="gwbasic">10 COLOR 0,0,2,2: REM BLACK BACKGROUND AND BORDER, RED TEXT AND EXTRA COLOR
20 GRAPHIC 2:SCNCLR:REM SELECT HI-RES GRAPHICS AND CLEAR THE SCREEN
30 POINT 2,640,640:REM DRAW A POINT AT 640/1024*160,640/1024*160
40 GET K$:IF K$="" THEN 40: REM WAIT FOR KEYPRESS
50 GRAPHIC 0:REM BACK TO TEXT MODE</langsyntaxhighlight>
 
 
Line 688 ⟶ 703:
To create graphics via manual memory manipulation, it's necessary to understand the layout of the screen. Unlike many other bitmap systems, the C64's bitmap was broken up into the 8x8-pixel cells used for text characters, even in high-resolution graphics mode. So while the first byte of video RAM unsurprisingly contains the leftmost 8 pixels on the top row of the screen, the second byte contains the 8 pixels directly ''below'' those, and so on for eight rows, before the ninth byte returns us to the top row for its 9th through 16th pixels. This requires calculation on the programmer's part to translate X,Y coordinates into a specific memory address/value combination (lines 30 through 60).
 
<langsyntaxhighlight lang="gwbasic">10 REM PLOT A RED PIXEL AT 100,100
20 REM INITIALIZE BITMAP MODE
21 POKE 53280,0:PRINT CHR$(147);"CLEARING BITMAP... PLEASE WAIT..."
Line 704 ⟶ 719:
75 REM CLEAR PIXEL, RETURN TO TEXT MODE
80 POKE MEM,0:POKE 53265,PEEK(53265) AND 223:POKE 53272,PEEK(53272) AND 247
90 POKE 53280,14:POKE 53281,6:POKE 646,14:END</langsyntaxhighlight>
 
'''Example 3:''' Commodore 64 with SuperExpander Cartridge
Line 710 ⟶ 725:
Commodore released a version of the SuperExpander Cartridge for the C64 that was similar to the one for the VIC-20, but not identical. Besides some differences in the actual BASIC statements, it also does not have the artificial scale factor; graphics statements instead take physical pixel coordinates.
 
<langsyntaxhighlight basiclang="gwbasic">10 COLOR 0,2,,,0: REM SET BACKGROUND AND BORDER TO BLACK, FOREGROUND TO RED
20 GRAPHIC 2,1:REM SELECT HIRES GRAPHICS AND CLEAR THE SCREEN
30 DRAW 1,100,100:REM DRAW A PIXEL AT 100,100
40 GET K$:IF K$="" THEN 40:REM WAIT FOR A KEYPRESS
50 GRAPHIC 0:REM RETURN TO TEXT MODE</langsyntaxhighlight>
 
 
'''Example 4:''' Commodore Plus/4, C-16, and 128 (40-column display)
 
By the time the Commodore Plus/4 and C-16 were released, Commodore was ready to ship them with a version of BASIC, dubbed BASICversion 3.5, that included graphic support out of the box. The statements were again similar to but slightly different from what had come before in the SuperExpander cartridges. When the Commodore 128 was released, its version of BASIC, dubbed 7.0, included all of 3.5 essentially unchanged, along with many other improvements.
 
<langsyntaxhighlight basiclang="gwbasic">10 COLOR 0,1:COLOR 1,3: REM SET BACKGROUND TO BLACK AND PIXEL COLOR TO RED
15 GRAPHIC 1,1 : REM ENTER BITMAP GRAPHICS MODE AND CLEAR SCREEN
20 DRAW 1,100,100 : REM PLOT PIXEL AT 100,100
30 GETKEY K$: REM WAIT FOR KEYPRESS THE NEW WAY
40 GRAPHIC 0,1 : REM RETURN TO TEXT MODE</langsyntaxhighlight>
 
'''Example 5:''' Commodore 128 (80-column display)
 
While the Commodore 128's BASIC 7.0 had impressive support for graphics on the 40-column display, it had no such support for bitmap graphics on the double-resolution 80-column display provided by its VDC chip. For that, we turn to BASIC 8, which was released as a separate software package that had to be loaded (or installed as a chip on the C128's motherboard). It was developed outside of Commodore and has a syntax very different from the standard BASIC's graphic commands, but it supports 3D graphics out of the box, including viewport clipping, scaling, etc. Note that the below program specifies a Z coordinate of 0 for the dot, which places it on the surface of the viewport.
 
<langsyntaxhighlight basiclang="gwbasic">10 @MODE,0:REM SELECT SCREEN SET
20 @COLOR,0,9,0:REM BACKGROUND BLACK, FOREGROUND BRIGHT RED
30 @SCREEN,0,0:REM SELECT MONOCHROME MODE
Line 739 ⟶ 754:
70 @DOT,100,100,0:REM DRAW DOT
80 GETKEY K$:REM WAIT FOR KEYPRESS
90 @TEXT:REM BACK TO TEXT MODE</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 SET VIDEO X 40:SET VIDEO Y 26:SET VIDEO MODE 5:SET VIDEO COLOR 0
110 OPEN #101:"video:"
120 DISPLAY #101:AT 1 FROM 1 TO 26
130 SET PALETTE BLACK,RED
140 PLOT 100,100</syntaxhighlight>
 
=={{header|C}}==
Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
#include<graphics.h>
 
int main()
{
initwindow(320,240,"Red Pixel");
putpixel(100,100,RED);
getch();
return 0;
}
</syntaxhighlight>
 
===Plain Xlib version===
Which is why most people use a framework :)
<syntaxhighlight lang="c">// dotrosetta.c - https://rosettacode.org/wiki/Draw_a_pixel
 
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
 
int
main(void)
{
Atom wm_both_protocols[1];
Atom wm_delete;
Atom wm_protocols;
Display *display;
GC gc;
Window root;
Window window;
XEvent event;
XSetWindowAttributes attr;
int more = 1;
 
display = XOpenDisplay(NULL);
if(display == NULL)
{
fprintf(stderr,"Error: The display cannot be opened\n");
exit(1);
}
root = DefaultRootWindow(display);
wm_delete = XInternAtom(display, "WM_DELETE_WINDOW", False);
wm_protocols = XInternAtom(display, "WM_PROTOCOLS", False);
attr.background_pixel = 0x000000;
attr.event_mask = ExposureMask;
window = XCreateWindow(display, root,
0, 0, 320, 240, 0,
CopyFromParent, InputOutput, CopyFromParent,
CWBackPixel | CWEventMask,
&attr
);
XStoreName(display, window, "Draw a Pixel");
wm_both_protocols[0] = wm_delete;
XSetWMProtocols(display, window, wm_both_protocols, 1);
gc = XCreateGC(display, window, 0, NULL);
XSetForeground(display, gc, 0xFF0000);
XMapWindow(display, window);
 
while(more)
{
XNextEvent(display, &event);
switch(event.type)
{
case Expose:
XDrawPoint(display, window, gc, 100, 100);
break;
 
case ClientMessage: // for close request from WM
if(event.xclient.window == window &&
event.xclient.message_type == wm_protocols &&
event.xclient.format == 32 &&
event.xclient.data.l[0] == wm_delete)
{
more = 0;
}
break;
 
default:
printf("unexpected event.type %d\n", event.type);;
}
}
 
XCloseDisplay(display);
exit(0);
}</syntaxhighlight>
 
=={{header|Delphi}}==
Line 746 ⟶ 859:
{{libheader| SysUtils}}
Console version using windows api, thanks for RRUZ ([https://stackoverflow.com/questions/10180016/creating-forms-without-using-vcl])
<syntaxhighlight lang="delphi">
<lang Delphi>
program Draw_a_pixel;
 
Line 832 ⟶ 945:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
[https://ibb.co/VL9Qs5z]
 
=={{header|EasyLang}}==
The Easylang graphic does not work with pixels. Although the drawing area is set to be 100x100 units - not pixels - this can be scaled and it can be drawn on intermediate positions. A pixel can be simulated by a small filled square.
 
<syntaxhighlight lang="easylang">
color 900
move 50 50
rect 0.5 0.5
</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
 
Uses Windows Forms
<langsyntaxhighlight lang="fsharp">open System.Windows.Forms
open System.Drawing
 
Line 846 ⟶ 968:
f.Size <- new Size(320,240)
f.Paint.Add(fun e -> e.Graphics.FillRectangle(Brushes.Red, 100, 100 ,1,1))
Application.Run(f)</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors arrays images images.testing images.viewer
kernel literals math sequences ;
IN: rosetta-code.draw-pixel
Line 858 ⟶ 980:
[ set-pixel-at ] keep image-window ;
 
MAIN: draw-pixel</langsyntaxhighlight>
 
 
=={{header|Evaldraw}}==
Assumes you resize your Evaldraw instance to 320x240 or open it from commandline with "evaldraw program.kc /320x240"
<syntaxhighlight lang="c">
()
cls(0);
setcol(255,0,0);
setpix(100,100);
</syntaxhighlight>
 
=={{header|Forth}}==
Line 868 ⟶ 997:
{{libheader|SDL2}}
 
<langsyntaxhighlight lang="forth">\ Bindings to SDL2 functions
s" SDL2" add-lib
\c #include <SDL2/SDL.h>
Line 893 ⟶ 1,022:
;
 
pixel</langsyntaxhighlight>
 
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 27-06-2018
' compile with: fbc -s console
' or: fbc -s gui
Line 925 ⟶ 1,054:
WindowTitle IIf(depth = 8, "Palette","True Color") + ", hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">i = new image[320,240]
i.setPixel[100,100,1,0,0]
i.show[]</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 320, 240 )
window 1, @"Single Pixel", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
oval fill fn CGRectMake( 100-0.5, 100-0.5, 1, 1 ), fn ColorRed
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select ( ev )
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
 
fn BuildWindow
 
HandleEvents
</syntaxhighlight>
 
 
=={{header|GB BASIC}}==
The resolution of the Game Boy's screen is only 160×144, so the window requirement is out, but the pixel is easy. Also, GB BASIC doesn't support displaying the color red, so the Game Boy's grayscale equivalent, dark gray, is used instead:
<langsyntaxhighlight GBlang="gb BASICbasic">10 color 1
20 point 100,100</langsyntaxhighlight>
 
=={{header|GML}}==
<syntaxhighlight lang GML="gml">draw_point(100, 100);</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 963 ⟶ 1,119:
fmt.Println("The color of the pixel at ( 0, 0) is", cmap[c1], "\b.")
fmt.Println("The color of the pixel at (100, 100) is", cmap[c2], "\b.")
}</langsyntaxhighlight>
 
{{out}}
Line 971 ⟶ 1,127:
</pre>
Create a PNG image?
::<langsyntaxhighlight lang="go">package main
 
import (
Line 993 ⟶ 1,149:
png.Encode(w, img)
}
</syntaxhighlight>
</lang>
GUI "window" rather than an image?
 
::<langsyntaxhighlight lang="go">package main
// first run" go get github.com/zserge/webview"
// simple GUI "window"
Line 1,005 ⟶ 1,161:
webview.Open("Minimal webview example",
"https://en.m.wikipedia.org/wiki/Main_Page", 320, 240, true)
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="unicon">#
# draw-pixel.icn
#
Line 1,016 ⟶ 1,172:
DrawPoint(100, 100)
Event()
end</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<lang IS-BASIC>100 SET VIDEO X 40:SET VIDEO Y 26:SET VIDEO MODE 5:SET VIDEO COLOR 0
110 OPEN #101:"video:"
120 DISPLAY #101:AT 1 FROM 1 TO 26
130 SET PALETTE BLACK,RED
140 PLOT 100,100</lang>
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">require 'gl2'
coinsert 'jgl2'
wd'pc Rosetta closeok;cc task isidraw; set task wh 320 200;pshow'
glpaint glpixel 100 100 [ glpen 1 1 [ glrgb 255 0 0 [ glclear ''
</syntaxhighlight>
</lang>
 
Note that this single pixel tends to be invisible or almost invisible on "modern" screens. You may wish to change this to use <code>glpen 4 1</code> if this is a problem on your system (though that technically would violate the requirements of this task, since we would not longer be drawing a single red pixel).
 
=={{header|Java}}==
Basic Implementation via subclass of JFrame:
<langsyntaxhighlight Javalang="java">import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
Line 1,054 ⟶ 1,205:
}
}
</syntaxhighlight>
</lang>
Advanced Implementation via subclass of JPanel (more powerful especially while repainting):
<langsyntaxhighlight Javalang="java">import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
Line 1,089 ⟶ 1,240:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Gtk, Graphics
const can = @GtkCanvas()
Line 1,110 ⟶ 1,261:
signal_connect(endit, win, :destroy)
wait(cond)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
This task seems very similar to the [[Bitmap]] task and so therefore is the code to accomplish it.
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
import java.awt.Color
Line 1,144 ⟶ 1,295:
println(if (c == Color.red) "red" else "white")
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,152 ⟶ 1,303:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
1) html/css
 
Line 1,184 ⟶ 1,335:
}
 
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
Line 1,190 ⟶ 1,341:
The luasdl2 library is required.
 
<langsyntaxhighlight lang="lua">local SDL = require "SDL"
 
local ret = SDL.init { SDL.flags.Video }
Line 1,207 ⟶ 1,358:
 
SDL.delay(5000)
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>CreateWindow[PaletteNotebook[{Graphics[{Red, Point[{100, 100}]}]}], WindowSize -> {320, 240}]</lang>
 
=={{header|M2000 Interpreter}}==
Line 1,221 ⟶ 1,369:
There is also a PSet statement for pixel drawing (from 9.5 version). Just replace the module call to PlotPixel with Pset.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
 
 
Line 1,258 ⟶ 1,406:
}
CheckIt
</syntaxhighlight>
</lang>
[https://www.dropbox.com/s/uscltz6pwy06jxv/DrawPixelForm.png?dl=0 OutPut]
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">CreateWindow[PaletteNotebook[{Graphics[{Red, Point[{100, 100}]}]}], WindowSize -> {320, 240}]</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Matrix that represents the window and the pixel positioned in it */
ematrix(320,240,1,100,100)$
 
/* Drawing the window and the pixel as needed */
wxdraw2d(palette = [white,gray,red], image(%,0,0,320,240))$
</syntaxhighlight>
[[File:DrawPixelMaxima.png|thumb|center]]
 
=={{header|Microsoft Small Basic}}==
<syntaxhighlight lang="smallbasic">GraphicsWindow.Width = 320
GraphicsWindow.Height = 240
GraphicsWindow.SetPixel(100, 100, GraphicsWindow.GetColorFromRGB(255, 0, 0))</syntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="MiniScript">gfx.clear color.black, 320, 240
gfx.setPixel 100, 100, rgb(255,0,0)</syntaxhighlight>
 
=={{header|Nim}}==
Line 1,265 ⟶ 1,435:
{{libheader|rapid}}
 
<langsyntaxhighlight lang="nim">import rapid/gfx
 
var
Line 1,282 ⟶ 1,452:
discard step # Prevent unused variable warnings
update step:
discard step</langsyntaxhighlight>
 
 
Line 1,288 ⟶ 1,458:
{{libheader|SDL2}}
Another version using SDL2.
<langsyntaxhighlight Nimlang="nim">import sdl2
 
discard sdl2.init(INIT_EVERYTHING)
Line 1,300 ⟶ 1,470:
renderer.present()
 
delay(5000)</langsyntaxhighlight>
 
=={{header|Oberon}}==
Line 1,306 ⟶ 1,476:
With basic module XYplane, the size of the drawing plane cannot be set. Tested with [https://miasap.se/obnc OBNC].
 
<langsyntaxhighlight Oberonlang="oberon">MODULE pixel;
 
IMPORT XYplane;
Line 1,315 ⟶ 1,485:
REPEAT UNTIL XYplane.Key() = "q"
END pixel.
</syntaxhighlight>
</lang>
 
=={{header|Objeck}}==
{{libheader|SDL2}}
<langsyntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
 
Line 1,366 ⟶ 1,536:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
Line 1,372 ⟶ 1,542:
Using the Graphics library provided with the standard OCaml distribution:
 
<langsyntaxhighlight lang="ocaml">module G = Graphics
 
let () =
Line 1,379 ⟶ 1,549:
G.set_color G.red;
G.plot 100 100;
ignore (G.read_key ())</langsyntaxhighlight>
run with:
<pre>$ ocaml graphics.cma draw_a_pixel.ml
Line 1,386 ⟶ 1,556:
{{libheader|OCamlSDL2}}
 
<langsyntaxhighlight lang="ocaml">open Sdl
 
let () =
Line 1,399 ⟶ 1,569:
Render.render_present renderer;
Timer.delay 3000;
Sdl.quit ()</langsyntaxhighlight>
run with:
<pre>$ ocaml -I $(ocamlfind query sdl2) sdl2.cma draw_a_pixel.ml
</pre>
 
=={{header|Odin}}==
 
<syntaxhighlight lang="odin">package main
 
import "vendor:sdl2"
 
main :: proc() {
using sdl2
 
window: ^Window = ---
renderer: ^Renderer = ---
event: Event = ---
 
Init(INIT_VIDEO)
CreateWindowAndRenderer(
640, 480,
WINDOW_SHOWN,
&window, &renderer
)
 
SetWindowTitle(window, "Empty window")
SetRenderDrawColor(renderer, 50, 255, 100, 255)
RenderDrawPoint(renderer, 100, 100)
RenderPresent(renderer)
 
for event.type != .QUIT {
Delay(10)
PollEvent(&event)
}
 
DestroyRenderer(renderer)
DestroyWindow(window)
Quit()
}</syntaxhighlight>
 
=={{header|Ol}}==
{{libheader|OpenGL}}
<langsyntaxhighlight lang="scheme">
(import (lib gl))
(import (OpenGL version-1-0))
Line 1,421 ⟶ 1,626:
(glVertex2f 100 100)
(glEnd)))
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
{{libheader|Gtk3}}
<langsyntaxhighlight lang="perl">use Gtk3 '-init';
 
my $window = Gtk3::Window->new();
Line 1,447 ⟶ 1,652:
$cr->rectangle( 100, 100, 1, 1);
$cr->stroke;
}</langsyntaxhighlight>
 
===Alternate with Perl/Tk===
 
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Draw_a_pixel
use warnings;
use Tk;
 
my $mw = MainWindow->new;
my $canvas = $mw->Canvas( -width => 320, -height => 240 )->pack;
$canvas->createRectangle( 100, 100, 100, 100, -outline => 'red' );
MainLoop;</syntaxhighlight>
 
===Alternate with X11::Protocol===
 
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Draw_a_pixel
use warnings;
use X11::Protocol;
 
my $x = new X11::Protocol or die "X11 connection failed";
$x->CreateWindow(my $id = $x->new_rsrc, $x->root, 'InputOutput',
$x->root_depth, 'CopyFromParent',
0, 0, 320, 240, 0,
background_pixel => 0x000000,
event_mask => $x->pack_event_mask( qw( Exposure )),
);
$x->ChangeProperty($id, $x->atom('WM_NAME'), $x->atom('STRING'),
8, 'Replace', 'Draw a Pixel');
$x->ChangeProperty($id, $x->atom('WM_PROTOCOLS'), $x->atom('ATOM'),
32, 'Replace', pack('L', $x->atom('WM_DELETE_WINDOW')));
$x->CreateGC(my $gc = $x->new_rsrc, $id, foreground => 0xff0000);
$x->MapWindow($id);
$x->event_handler('queue');
 
my ($more, $name, %e) = 1;
while($more)
{
%e = $x->next_event;
$name = $e{name};
EVENT->$name;
}
 
sub EVENT::Expose { $x->PolyPoint($id, $gc, 'ORIGIN', 100, 100) }
sub EVENT::ConfigureNotify { }
sub EVENT::ClientMessage
{
if($e{type} == $x->atom('WM_PROTOCOLS') &&
unpack('L', $e{data}) == $x->atom('WM_DELETE_WINDOW'))
{
$more = 0;
}
else
{
warn "Unknown $name, $e{type}\n";
}
}
sub EVENT::AUTOLOAD { die "Sorry, no handler for $name\n" }</syntaxhighlight>
 
=={{header|Phix}}==
Line 1,453 ⟶ 1,718:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/drawapixel.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,487 ⟶ 1,752:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Plain English}}==
<syntaxhighlight lang="text">
To run:
Start up.
Make a box 320 pixels by 240 pixels.
Draw the box with the white color and the white color.
Make a spot with 100 pixels and 100 pixels.
Draw the spot with the red color.
Refresh the screen.
Wait for the escape key.
Shut down.
</syntaxhighlight>
 
=={{header|Processing}}==
Line 1,493 ⟶ 1,771:
A static-mode sketch containing setting one pixel on the canvas pixels array:
 
<langsyntaxhighlight Processinglang="processing">size(320, 240);
set(100, 100, color(255,0,0));</langsyntaxhighlight>
 
The same sketch in active mode sketch is:
 
<langsyntaxhighlight Processinglang="processing">void setup() {
size(320, 240);
set(100, 100, color(255,0,0));
}</langsyntaxhighlight>
 
Or a pixel can be manipulated through loading and modifying the pixels array. The formula to access a pixel is x + y * image width.
 
<langsyntaxhighlight Processinglang="processing">void setup() {
size(320, 240);
loadPixels();
pixels[width*100 + 100] = color(255,0,0);
updatePixels();
}</langsyntaxhighlight>
 
Processing can also draw a dot on the canvas using the `point()` command will also draw a dot on the canvas...
 
<langsyntaxhighlight Processinglang="processing">size(320, 240);
stroke(color(255,0,0));
point(100, 100);</langsyntaxhighlight>
 
...however, whether a point corresponds to a single pixel on the screen may depend on device-specific factors such as `pixelDensity()`, render features such as 2D / 3D mode or `smooth()`, or style settings such as `strokeWidth()` or `strokeCap()`.
Line 1,522 ⟶ 1,800:
=={{header|PureBasic}}==
 
<syntaxhighlight lang="purebasic">
<lang PureBasic>
If OpenWindow(0, 0, 0, 320, 240, "Rosetta Code Draw A Pixel in PureBasic")
If CreateImage(0, 320, 240) And StartDrawing(ImageOutput(0))
Line 1,533 ⟶ 1,811:
Until Event = #PB_Event_CloseWindow
EndIf
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
{{works with|Python |2.7.14}}
{{libheader|PIL}}
<langsyntaxhighlight Pythonlang="python">from PIL import Image
 
img = Image.new('RGB', (320, 240))
Line 1,544 ⟶ 1,822:
pixels[100,100] = (255,0,0)
img.show()
</syntaxhighlight>
</lang>
 
=={{header|QB64}}==
<langsyntaxhighlight lang="qb64">Screen _NewImage(320, 240, 32)
PSet (100, 100), _RGB(255, 0, 0)</langsyntaxhighlight>
 
=={{header|QBasic}}==
<langsyntaxhighlight lang="qbasic">' http://rosettacode.org/wiki/Draw_a_pixel
' This program can run in QBASIC, QuickBASIC, gw-BASIC (adding line numbers) and VB-DOS
SCREEN 1
COLOR , 0
PSET (100, 100), 2
END</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require racket/draw)
(let ((b (make-object bitmap% 320 240)))
(send b set-argb-pixels 100 100 1 1 (bytes 255 0 0 255))
b)</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,570 ⟶ 1,848:
Really? Draw a single pixel? Sigh.
 
<syntaxhighlight lang="raku" perl6line>use GTK::Simple;
use GTK::Simple::DrawingArea;
use Cairo;
Line 1,591 ⟶ 1,869:
 
my $ctx = $da.add-draw-handler( &rect-do );
$app.run;</langsyntaxhighlight>
 
=={{header|ReScript}}==
 
<langsyntaxhighlight lang="rescript">type document // abstract type for a document object
type context = { mutable fillStyle: string, }
 
@bs.val external doc: document = "document"
 
@bs.send external getElementById: (document, string) => Dom.element = "getElementById"
@bs.send external getContext: (Dom.element, string) => context = "getContext"
 
@bs.send external fillRect: (context, int, int, int, int) => unit = "fillRect"
 
let canvas = getElementById(doc, "my_canvas")
Line 1,609 ⟶ 1,887:
 
ctx.fillStyle = "#F00"
fillRect(ctx, 100, 100, 1, 1)</langsyntaxhighlight>
 
 
<langsyntaxhighlight lang="html"><!DOCTYPE html>
<html>
<head>
Line 1,641 ⟶ 1,919:
 
</body>
</html></langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,647 ⟶ 1,925:
:::* &nbsp; PC/REXX
:::* &nbsp; Personal/REXX
<langsyntaxhighlight lang="rexx">/*REXX program displays (draws) a pixel at a specified screen location in the color red.*/
parse upper version !ver .
!pcrexx= 'REXX/PERSONAL'==!ver | 'REXX/PC'==!ver /*obtain the REXX interpreter version. */
Line 1,660 ⟶ 1,938:
end
 
call scrWrite x,y,txt,,,CC /*stick a fork in it, we're all done. */</langsyntaxhighlight>
<br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"># Project : Draw a pixel
 
load "guilib.ring"
Line 1,701 ⟶ 1,979:
endpaint()
}
label1 { setpicture(p1) show() }</langsyntaxhighlight>
Output image:
[https://www.dropbox.com/s/gbtrkx7bx7hogxm/PixelColor.jpg?dl=0 Draw a pixel]
Line 1,709 ⟶ 1,987:
 
One of the requirements (fixed screen resolution) could not be met.
<langsyntaxhighlight lang="robotic">
. "Set the sprite's reference character located at the"
. "upper-left corner of the board (char 0)"
Line 1,734 ⟶ 2,012:
. "Display the sprite at the given location"
put c0c Sprite p00 at "('xPos')" "('yPos')"
</syntaxhighlight>
</lang>
 
I highly recommend you check out [https://www.digitalmzx.net/wiki/index.php?title=Sprite_(Tutorial) this tutorial] for the usage of Sprites in order to get a better understanding of all this.
 
=={{header|RPL}}==
HP devices running RPL have only black-and-white LCD screens, so so much for the red color. Screen resolution goes from 137x32 to 131x80 depending on the model, but the user can freely define coordinates for the bottom-left pixel (minimum value) and for the top-right pixel (maximum value), so the program below works on any model:
≪ CLLCD (0,0) PMIN (320,240) PMAX (100,100) PIXEL ≫ EVAL
 
=={{header|Ruby}}==
Line 1,742 ⟶ 2,024:
{{libheader|gtk3}}
 
<langsyntaxhighlight lang="ruby">require 'gtk3'
 
Width, Height = 320, 240
Line 1,767 ⟶ 2,049:
window.show
Gtk.main
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
Line 1,776 ⟶ 2,058:
 
 
<langsyntaxhighlight lang="rust">extern crate piston_window;
extern crate image;
 
Line 1,810 ⟶ 2,092:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Line 1,817 ⟶ 2,099:
{{Out}}
Best experienced in your browser [https://scastie.scala-lang.org/AHtZh6zhRTWGj3M8azw28g with Scastie (remote JVM)].
<langsyntaxhighlight lang="scala">import java.awt.image.BufferedImage
import java.awt.Color
import scala.language.reflectiveCalls
Line 1,860 ⟶ 2,142:
println("Tests successfully completed with no errors found.")
 
}</langsyntaxhighlight>
 
=={{header|SmileBASIC}}==
I almost said that the 320x240 requirement was not going to happen. Then I realised the 3DS bottom screen is exactly 320x240.
<langsyntaxhighlight lang="smilebasic">XSCREEN 3
DISPLAY 1
GPSET 100, 100, RGB(255, 0, 0)
WAIT 60</langsyntaxhighlight>
 
=={{header|Standard ML}}==
Works with PolyML
<langsyntaxhighlight Standardlang="standard MLml">open XWindows ;
open Motif ;
 
Line 1,892 ⟶ 2,174:
)
end;</langsyntaxhighlight>
call
imgWindow () ;
Line 1,899 ⟶ 2,181:
{{works with|Tcl|8.5}}
==={{libheader|Tk}}===
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
pack [canvas .c -width 320 -height 240 -bg #fff] -anchor nw
.c create rectangle 100 100 100 100 -fill #f00 -outline ""</langsyntaxhighlight>
 
=={{header|True BASIC}}==
<langsyntaxhighlight lang="truebasic">SET WINDOW 0, 320, 0, 240
SET COLOR 4
PLOT 100,100
END</langsyntaxhighlight>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0}}
Run it using Uiua Pad to see the teeny-weeny dot.
<syntaxhighlight lang="Uiua">
↯240_320_3 0
⍜(⊡[100 100]|[1 0 0]◌)
</syntaxhighlight>
 
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">
( $ uxnasm draw-pixel.tal draw-pixel.rom && uxnemu draw-pixel.rom )
 
|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1
|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
 
|0100
( set theme )
#0f00 .System/r DEO2
#0000 .System/g DEO2
#0000 .System/b DEO2
 
( set screen size )
#0140 .Screen/width DEO2
#00f0 .Screen/height DEO2
 
( set position )
#0064 .Screen/x DEO2
#0064 .Screen/y DEO2
 
( draw pixel )
#01 .Screen/pixel DEO
BRK</syntaxhighlight>
 
=={{header|VBA}}==
Word
<langsyntaxhighlight lang="vb">Sub draw()
Dim sh As Shape, sl As Shape
Set sh = ActiveDocument.Shapes.AddCanvas(100, 100, 320, 240)
Set sl = sh.CanvasItems.AddLine(100, 100, 101, 100)
sl.Line.ForeColor.RGB = RGB(Red:=255, Green:=0, Blue:=0)
End Sub</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import gg
import gx
 
Line 1,937 ⟶ 2,252:
ctx.draw_pixel(100, 100, gx.red)
ctx.end()
}</langsyntaxhighlight>
 
=={{header|Wee Basic}}==
Since the resolution of the Nintendo DS's bottom screen is only 256×192 (the same applies to the top screen), the window requirement is out, but the pixel is easy:
<langsyntaxhighlight Weelang="wee Basicbasic">keyhide
plot 0 100,100,5
end</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas, Color
 
Line 1,967 ⟶ 2,282:
 
static getRGB(col) { "{%(col.r), %(col.g), %(col.b)}" }
}</langsyntaxhighlight>
 
{{out}}
Line 1,983 ⟶ 2,298:
so the pixel can be seen. Borland's tasm and tlink /t were used to
create an executable .com file.
<syntaxhighlight lang="x86">
<lang X86>
.model tiny
.code
Line 2,000 ⟶ 2,315:
ret ;return to OS
end start
</syntaxhighlight>
</lang>
 
=={{header|XPL0}}==
This is all that's required to plot a pixel on the Raspberry Pi version of XPL0. It can do tens of millions of them per second. (FB = Frame Buffer.)
<langsyntaxhighlight XPL0lang="xpl0">[SetFB(320, 240, 24); Point(100, 100, $FF0000)]</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">open window 320, 240
color 255, 0, 0
dot 100, 100</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
The ZX Spectrum screen is only 256x224 (unless you're running one of the games like Starion or Dark Star which used scary machine code timing tricks to draw on the border), meaning the window requirement is out, but the pixel is easy:
<syntaxhighlight lang ="zxbasic">PLOT INK 2;100,100</langsyntaxhighlight>
67

edits