介绍
有一天,您正在向孩子展示如何在计算机上绘画。您mspaint.exe在运行栏中键入。令您惊恐的是,“没有符合您搜索条件的项目”。您必须创建一个简单的绘画版本,以便您的孩子可以绘画!
挑战
您必须创建一个简单的绘图程序。为此,请打开白色显示窗口(大于99x99像素)。每当按下鼠标时,请将鼠标打开的像素更改为黑色。
这是代码高尔夫球,因此最短答案以字节为单位!
Pen正是这样做时:o
                有一天,您正在向孩子展示如何在计算机上绘画。您mspaint.exe在运行栏中键入。令您惊恐的是,“没有符合您搜索条件的项目”。您必须创建一个简单的绘画版本,以便您的孩子可以绘画!
您必须创建一个简单的绘图程序。为此,请打开白色显示窗口(大于99x99像素)。每当按下鼠标时,请将鼠标打开的像素更改为黑色。
这是代码高尔夫球,因此最短答案以字节为单位!
Pen正是这样做时:o
                Answers:
00000000  b8 0f 00 cd 10 b8 02 10  ba 18 01 cd 10 b8 03 00  |................|
00000010  cd 33 4b b8 01 0c eb f3  3f 00                    |.3K.....?.|
0000001a
假定存在VGA图形卡和任何Microsoft兼容的鼠标驱动程序。
-2个字节感谢@berendi!
            |   org 0x100
            |   use16
b8 0f 00    |       mov ax, 0x000f      ; set screen mode: 640x350 monochrome
cd 10       |       int 0x10            ; call video bios
b8 02 10    |       mov ax, 0x1002      ; set palette
ba 18 01    |       mov dx, palette     ; pointer to palette data
cd 10       |   @@: int 0x10            ; call video bios
b8 03 00    |       mov ax, 0x0003      ; get mouse position in (CX, DX) and button status in BL
cd 33       |       int 0x33            ; call mouse driver
4b          |       dec bx              ; if no buttons pressed, --BX = 0xFFFF (invalid page)
b8 01 0c    |       mov ax, 0x0c01      ; plot pixel with color AL at (CX, DX) in page BH
eb f3       |       jmp @b              ; repeat
3f 00       |   palette db 0x3f, 0x00   ; palette in 2:2:2 rgb. color 0 = white, 1 = black.
            |                           ; this should be a 17-byte struct, but we only care about the first two colors here
这里的所有都是它的。不涉及魔术,仅需几个函数调用。
INT 33h将在中返回0 BX,这将按预期减少为FFFFh,但仍在画一条线。也许是因为模式11h仅具有一个视频页面,并且无效的页码会自动得到修复?我正在VM VirtualBox上进行测试,所以也许这是其BIOS实施正在做的事情。
                    int 0x10,取而代之的jmp是第二个int 0x10。偏移量jmp保持不变,请调整mov dx
                    的HTML
<canvas id=c>
的JavaScript
onclick=e=>c.getContext`2d`.fillRect(e.x,e.y,1,1)
感谢Gustavo Rodrigues节省了2个字节,Shaggy节省了2个字节。
onclick=e=>c.getContext`2d`.fillRect(e.x,e.y,1,1)/* This css isn't needed for the program to work. */
*{margin: 0}
#c{border:1px solid black}<canvas id=c><canvas id=c>样
                    c.从JS开头删除来节省2个字节。@Anoplexian,这是将物体margin扔掉几个像素的默认主体。
                    谢谢,科迪和昆汀!我希望摆脱,#include <windows.h>但这会导致各种错误。至少它可以在多台显示器上正常工作...
#include<windows.h> 
struct{int h,m,w,x:16,y:16;}m;h;main(){for(h=CreateWindow("EDIT","",1<<28,0,0,199,199,0,0,0,0);GetMessage(&m,h,0,0);m.m^513?DispatchMessage(&m):SetPixel(GetDC(h),m.x,m.y,0));}
先前的代码:
#include<windows.h> 
main(){MSG m;HWND h;for(h=CreateWindow("EDIT","",1<<28,0,0,199,199,0,0,0,0);GetMessage(&m,h,0,0);m.message^513?DispatchMessage(&m):SetPixel(GetDC(h),LOWORD(m.lParam),HIWORD(m.lParam),0));}
h到for初始值设定项中以保存两个字节:)
                    LOWORD并HIWORD是不正确的方法来提取光标从包装的坐标LPARAM。您应该使用GET_X_LPARAM和GET_Y_LPARAM,否则在多台显示器上将得到错误的结果。
                    int main,它将再增加4个字节。
                    (short)LOWORD()和(short)HIWORD()会解决这个问题?
                    main函数的返回类型的原因(由于隐式int规则)。唯一的问题是,省略了return 0;C89和MSVC中未定义行为的原因。在C99和C ++中,您需要为的返回类型main,但返回值return 0;是隐式的,因此是不必要的。嗯,这不是打高尔夫球的语言。:-)
                    @dzaima节省了19个字节
void setup(){background(-1);}void draw(){if(mousePressed)point(mouseX,mouseY);}void setup() {
  size(100,100);            // size of the sketch
  background(-1);           // set the background to white
                            // 1 byte shorter than background(255);
}
void draw(){}               // required for mousePressed to work
void mousePressed() {       // function that is called whenever the mouse is pressed
  point(mouseX, mouseY);    // draw a point at the mouse's coordinates
                            // the colour is set to black as default
}size(100,100);不必提及,谢谢!(同样我也不知道为什么我看不到这个if(mousePressed)位)
                    经过镀铬测试。
onclick=e=>document.body.outerHTML+=`<a style=position:fixed;top:${e.y}px;left:${e.x}px;width:1px;height:1px;background:#000>`
onclick=e=>document.body.outerHTML+=`<a style=position:fixed;top:${e.y}px;left:${e.x}px;width:1px;height:1px;background:#000>`import Graphics.Gloss.Interface.Pure.Game
main=play FullScreen white 9[]Pictures(#)(\_->id)
EventKey(MouseButton LeftButton)Down _(x,y)#l=Translate x y(Circle 1):l
_#l=l
这将使用Gloss库(v1.11)。它会打开一个全屏窗口,并通过按鼠标左键绘制黑色像素(实际上是半径为1的圆圈)。按ESC退出。
这个怎么运作:
play                 -- handles the whole event handling, screen update, etc. process
    FullScreen       -- use a full screen window
    white            -- use a white background
    9                -- 9 simulation steps per second (see simulation handler below)
    []               -- the initial world state, an emtpy list of circles
    Pictures         -- a function to turn the world state into a picture.
                     -- The world state is a list of translated circles and
                     -- "Pictures" turns this list into a single picture
    (#)              -- event handler, see below
    (\_->id)         -- simulation step handler. Ignore time tick and return the
                     -- world state unchanged. More readable: "\tick world -> world"
EventKey ... # l=... -- if the left mouse button is pressed, add a circle with
                     -- radius 1 translated to the current position of the mouse
                     -- to the world state
_#l=l                -- on all other events do nothing
编辑:@ceased打开了counterclockwis告诉我有关光泽的最新版本,使用全屏窗口时它可以节省5个字节。谢谢!
FullScreen在光泽度> = 1.11时为零,将分数降低了5。(虽然我遇到了错误unknown GLUT entry glutInit,但是我的过剩设置可能无法正常工作;从未在这台机器上使用过。)
                    在SmileBASIC的爸爸,佩蒂特电脑这一个运行(并且是一个两字节的短。)
PNLTYPE"OFF
GPAGE 1GCLS 15@A
GPSET TCHX,TCHY
GOTO@A
说明:
PNLTYPE"OFF       'turn off keyboard
GPAGE 1           'set graphics to lower screen
GCLS 15           'clear with color 15 (white)
@A                'loop begin
GPSET TCHX,TCHY   'set black pixel at draw location
GOTO@A            'loop
GPSET TCHX,TCHY:GOTO@A由于默认颜色为0(透明),因此可以节省1个字节。
                    d=$(document.body).click((e)=>d.append("<div style='background:black;width:5;height:5;position:fixed;top:"+e.pageY+";left:"+e.pageX+"'>"))
添加单击和拖动支持将很简单,您可以设置一个变量,但是,根据说明并为缩短代码,不支持“每次按下鼠标时,将鼠标打开的像素更改为黑色”。 。我将指令解释为click事件。
1 CLS 15 
2 IF MOUSEBTN=1 THEN PLOT MOUSEx,MOUSEy
3 GO TO 2
黑色已经是默认的笔颜色,但是背景通常是令人讨厌的灰色阴影,因此CLS 15请将其设置为亮白色。
GOTO成为GO TO)后添加所有空格
                    XSCREEN 4GCLS-1@L
TOUCH OUT,X,Y
GPSET X,Y+240,0GOTO@L
解释:
XSCREEN 4 'display one graphics page on both screens
GCLS -1 'fill screen with white
@L 'loop
TOUCH OUT,X,Y 'get touch location
GPSET X,Y+240,0 'draw black pixel at touch position, offset by the height of the top screen.
GOTO @L 'loop
-20个字节,感谢@cfrick指出我可以使用它use来缩小导入。
(use 'quil.core)(defsketch P :mouse-dragged #(point(mouse-x)(mouse-y)))使用Quil库。每当拖动鼠标时绘制一个点。
基本上是处理答案,因为Quil是Clojure的处理包装器。
(q/defsketch P ; Start a new drawing
             :mouse-dragged ; Attach a mouse drag listener
             ; that draws a point at the current mouse location
             #(q/point (q/mouse-x) (q/mouse-y))) (use 'quil.core)(defsketch P :mouse-dragged #(point(mouse-x)(mouse-y)))
                    use因为它“ 不合适”。谢谢。
                    import java.awt.*;import java.awt.event.*;class M{static int x,y;public static void main(String[]a){Frame f=new Frame(){public void paint(Graphics g){g.drawLine(x,y,x,y);}};f.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e){x=e.getX();y=e.getY();f.repaint(x,y,1,1);}});f.setBackground(Color.WHITE);f.resize(500,500);f.show();}}
取消高尔夫:
import java.awt.*;
import java.awt.event.*;
class M {
    //store the last point
    static int x, y;
    public static void main(String[] a) {
        Frame f = new Frame() {
            @Override
            public void paint(Graphics g) {
                g.drawLine(x, y, x, y);
            }
        }
        f.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                x = e.getX();
                y = e.getY();
                //repaint only the "new pixel"
                f.repaint(x, y, 1, 1);
            }
        });
        //default background is grey
        f.setBackground(Color.WHITE);
        f.resize(500, 500);
        f.show();
    }
}
from turtle import *;up();onscreenclick(lambda x,y: goto(x,y)or dot(1));mainloop()
使用Turtle模块,这还不错:)
取消高尔夫:
import turtle
turtle.up()
def fun(x, y):
    turtle.goto(x,y)
    turtle.dot(1)
turtle.onscreenclick(fun)
turtle.mainloop()
如果可以在点之间有线,则实际上可以节省5个字节:
from turtle import *;onscreenclick(lambda x,y: goto(x,y)or dot(1));mainloop()
CSS的乐趣box-shadow!
s='0 0 0',onclick=e=>p.style.boxShadow=s+=`,${e.x}px ${e.y}px 0`*{margin:0;width:1px;height:1px<p id=p>此人们试图抵消默认margin的的<body>元素,但它可以是不同的浏览器和用户代理样式表不同。已在Chrome中成功测试。
s='0 0 0',onclick=e=>p.style.boxShadow=s+=`,${e.x-8}px ${e.y-16}px 0`*{width:1px;height:1px<p id=p>在半像素坐标处绘制时,此像素可能显得更大。
s='0 0 0',onclick=e=>p.style.boxShadow=s+=`,${e.x}px ${e.y}px 0 .5px`*{margin:0;width:0<p id=p>Pt-Change(
我敢肯定,大多数使用TI calcs的人都想到了该Pen命令,但事实证明,它没有标记,因此将其计为3个字节是最有意义的。您可以Pt-Change(在http://tibasicdev.wikidot.com/pt-change上了解有关交互式版本的信息。
高尔夫版
l=love v,g,m,p=255,l.graphics,l.mouse,{}d=m.isDown function l.draw()g.setBackgroundColor(v,v,v)if d(1)or d(2)then p[#p+1],p[#p+2]=m.getPosition()end g.setColor(0,0,0)g.points(p)end不打高尔夫球
l=love 
v,g,m,p=255,l.graphics,l.mouse,{}
d=m.isDown 
function l.draw()
  g.setBackgroundColor(v,v,v)
  if d(1)or d(2)then 
    p[#p+1],p[#p+2]=m.getPosition()
  end 
  g.setColor(0,0,0)
  g.points(p)
end相当容易。首先进行一些初始化,以简化操作。在它之后,它将检查单击了mous,并将点保存到数组中。之后,它绘制点。还将颜色设置为黑白,原因是默认情况下是这样:)
 
这是图片:
l=love v,g,m,p=255,l.graphics,l.mouse,{}function l.draw()g.setBackgroundColor(v,v,v)if m.isDown(1)then p[#p+1],p[#p+2]=m.getPosition()end g.setColor(0,0,0)g.points(p)end这将导致169个字节!
                    旧帖子,但发现这很有趣。
由于处理已经完成,因此我决定做一些不同的事情。  
这将绘制是否按下鼠标,因此不是pydude要求的,而是几乎。
#ifdef GL_ES
precision lowp float;
#endif
uniform float time;uniform vec2 mouse,resolution;uniform sampler2D t;void main(){vec2 f=gl_FragCoord.xy,r=resolution;gl_FragColor=vec4(time<2.||length(f-mouse*r)>2.&&texture2D(t,f/r).x>.1);}
setbg 7
setpc 0
ht
pu
make "buttonact[setpos clickpos pd fd 1 pu]
说明(buttonact每次单击按钮都会执行分配给变量的代码):
SetBackground 7 ; white
SetPenColor 0 ; black
HideTurtle
PenUp
make "buttonact [
  SetPos ClickPos
  PenDown
  Forward 1
  PenUp
]
至少,我认为这可行。它应该根据文档,但是显然buttonact变量只能在wxWidgets构建中工作,而我正在努力在现代Linux上使用wxWidgets构建UCBLogo(wandaamean,您无法int向后投射指针吗?)。
要查看预期的效果,可以使用无限循环运行此较长的版本。
setbg 7 setpc 0 ht pu
while[1=1][setpos mousepos if[button?][pd] fd 1 pu]
但这会ucblogo在我的机器上崩溃...看起来像是在竞争。
setbg 7 setpc 0 ht pu
while[1=1][setpos mousepos if[button?][pd] fd 1 pu wait 1]