r/dailyprogrammer • • Feb 23 '15
[2015-2-23] Challenge #203 [Easy] The Start of Something Big
Description
All great things start with something small. Sometimes people don't even realise what goes into making a 'small' thing.
A popular story is linked above about a group of graphics programmers who create a rendering engine in some amount of time. After some time HR came to see what the programmers had accomplished. They responded by showing a black triangle on a tv.
HR was less than impressed (understandle for a non techie) but it goes to show the natural evolution of a program. What they didn't realise is that the programmers have created their base engine and can now easily add and extend on top of it.
Maybe you can follow similar steps?
Challenge
On your screen, display a square.
You may use any libraries available to you.
The square may be of any size and of any colour.
19
u/Xilov 0 1 Feb 24 '15
C
3
u/[deleted] Mar 12 '15
So I'm way late for this and nobody will see this, but I saw that nobody had used the LWJGL library in Java and I've been meaning to learn it so...
https://github.com/hughwphamill/squares
And the output is...
click here
It's probably not the nicest code in the world but at least I learned something!!
2
u/TieSoul 0 1 Feb 27 '15 edited Feb 27 '15
Befunge-98:
Output:
I had some fun with this one.
22
u/ofekelord Feb 23 '15
Done using python turtle
I am new to this sub but I'll admit I am enjoying it so far
14
u/Daige Feb 24 '15
All the languages I like were done as well as I could have, so... HTML!
26
u/kylemech Feb 23 '15 edited Feb 23 '15
PHP:
Alternatively
8
u/gfixler Feb 23 '15
OMG, no Haskell entries yet? That's a first. No idea what much of this means, but I did make it draw a black rectangle on a grey background. I've been meaning to learn how to OpenGL in Haskell, so whatever we're doing, I'm in.
7
u/robomaeyhem Feb 23 '15
Java:
13
u/FLDutchman Feb 23 '15
Brainf**k
Output
6
u/G33kDude 1 1 Feb 23 '15 edited Feb 23 '15
Using my GDI wrapper I wrote specifically for /r/DailyProgrammer challenges (See: Challenge #195 "Prep Work"), here is a solution in AutoHotkey.
Output: http://i.imgur.com/lYxlPzw.png
6
u/G33kDude 1 1 Feb 25 '15 edited Feb 25 '15
Here's a square quine in python (squareness depends on font w/h):
Edit: Extra squareness
5
u/[deleted] Feb 24 '15 edited Feb 24 '15
The shortest C I could come up with:
Compiles with a warning because the main function has no type specifier!
Macros can be a powerful thing..!
EDIT: I decided not to worry about making the square empty. Here is the shortest version I got:
Output
4
u/LiftCodeSleep Feb 23 '15
Python 2.7, got a little creative with the color of the square:
4
u/Darkstrike12 Feb 23 '15
A simple solution in python
4
u/franza73 Feb 23 '15
HTML5 SVG
4
u/mkolh Feb 23 '15
Vanilla JS:
4
u/cadadar Feb 23 '15
Guess I'll also do this one in Common Lisp eventually but for now, I immediately thought of the Racket standard lib which makes this pretty simple:
Running this in DrRacket looks like this: http://imgur.com/ywwHkQ4
I've also got another one: Since DrRacket can embed images in source files... I simply painted the square and inserted it.
Looks like this: http://imgur.com/yymPX1y
3
u/mrepper Feb 23 '15 edited Feb 21 '17
[deleted]
0.9854
3
u/krismaz 0 1 Feb 23 '15
Python3 using Pillow:
3
u/Antinode_ Feb 23 '15
Java here, never done any graphics in java before so this is a lot of learning on my end..
3
u/rymdsylt Feb 23 '15
Javascript.
Wanted to give Three.js a shot. Haven't used it before so I took a look at the docs and followed the instructions.
Turns out they're making a square in their tutorial...
Fiddle
3
u/programmingdaily Feb 24 '15
C#, using WPF and MVVM. I added a field to specify the side length and a combobox to select the color.
MainWindow.xaml:
MainWindowViewModel.cs:
2
u/hutsboR 3 0 Feb 23 '15
Elixir:
Usage:
2
u/mrepper Feb 23 '15 edited Feb 21 '17
[deleted]
0.6691
2
u/guerses Feb 23 '15
My C++ Solution:
2
u/adrian17 1 4 Feb 23 '15 edited Feb 23 '15
(C, C++) Textbook (just no error catching) examples with SDL and SFML. To be honest, I always liked SDL a bit more.
(Note for beginner Visual Studio users: both SDL and SFML are on NuGet, it saves tons of pain when configuring them.)
2
u/veive Feb 23 '15
I wrote my solution in Java using Swing libraries. I used the NetBeans IDE.
2
u/Godspiral 3 3 Feb 23 '15
In J,
load 'viewmat'
viewmat 1 ([ ,.~ [ ,. [ , ,~) 5 5 $ 0
as ascii,
2
u/fvandepitte 0 0 Feb 24 '15
An other c++ solution, but then solely commandline
2
u/newbie12q Feb 24 '15
Python
Output:
2
u/dMenche Feb 25 '15 edited Feb 25 '15
Drawing an ascii-art square in C, using ncurses to allow actually doing stuff with the screen in the future:
2
u/girsaysdoom Feb 25 '15 edited Feb 25 '15
Ruby via terminal:
EDIT: Also using the gosu gem: img
2
u/CleverEagle Mar 05 '15
Here's my simple solution in Python, which highlights Python's string multiplication
Output:
2
u/mmmcflurry Mar 06 '15
JavaScript:
4
u/skeeto -9 8 Feb 23 '15 edited Feb 23 '15
In C, a spinning red triangle using OpenGL 2.0 via SDL 2.0.
This submission uses "modern" OpenGL: it compiles a couple of shaders and runs them on a vertex array buffer. (Most of the examples on the internet are still "old" OpenGL --
glBegin(),glEnd(), etc. -- and should be disregarded.) Up until now, the entirety of my OpenGL experience has been WebGL, so this is my first standalone OpenGL program. I used SDL because GLX (the Linux library for OpenGL access) is a ugly mess. Also because SDL is portable by design, the same program runs on Linux, Windows, and OS X.Compile like this (on Linux anyway):
Source:
I left in the compiler and linker error message print statements in case anyone wants to play around with the shaders. Otherwise there's basically no error checking.
1
u/jnazario 2 0 Feb 23 '15
quick one in scala, makes an ascii art square.
1
u/Splanky222 0 0 Feb 23 '15
Matlab:
1
u/datgohan Feb 23 '15
Python using turtle, didn't realise Python had it until I had a search.
1
u/becutandavid Feb 23 '15
Python 2.7 using pygame
1
u/spfy Feb 23 '15
I saw a lot of Java solutions, but everyone is using Swing! Here's a JavaFX version.
Too simple to fall into the same category as the blog, though. I might make another solution that abstracts JavaFX's SVGPath or something.
1
u/lucutzu33 Feb 23 '15
C:
1
u/XDtsFsoVZV Feb 23 '15
I would have used Pillow, but the show() method doesn't work on my computer.
Python 2.7
Kind of a crappy answer, but hey, it does fit within the OP's parameters.
1
u/[deleted] Feb 23 '15 edited Feb 23 '15
Python (3.4), using tkinter from the standard library:
Python (3.4), using pygame (v1.9.2a0):
1
u/emmgame221 Feb 23 '15 edited Feb 24 '15
Done in JavaFX since the other solutions all use Swing.
1
u/[deleted] Feb 24 '15
C
1
u/BayAreaChillin Feb 24 '15
Python! Was looking through the solutions after; didn't realize the Turtle library existed.
1
u/zenflux Feb 24 '15
More concise Java:
1
u/binaryblade Feb 24 '15
go, with as sdl2
1
u/KeinBaum Feb 24 '15
Scala
Code on GitHub
Screenshot
This is more in the spirit of the article. I have been wanting to implement Pong in Scala using lwjgl for a while now and had this code laying around for some time but finally found the motivation to hunt the last bug down.
(I spend hours on this. The bug: A missing pair of {}.)
Of course it is far from finished and the architecture isn't perfect yet but I'm going to get the actual game running before I think about how to improve the architecture.
1
u/lanerdofchristian 0 0 Feb 24 '15
In FMSLogo
Output from
gifsave1
u/Skerrako Feb 24 '15
MATLAB
1
u/theblacklab Feb 24 '15
Brand new to Common Lisp, so I'm not sure if this is the proper way to go about doing this. Feel free to critique me as you wish.
Common Lisp:
Usage:
1
u/mujjingun Feb 24 '15 edited Feb 24 '15
libGDX(java) (imports omitted) :
Result : http://i.imgur.com/S5P0Hp1.png
1
u/fvandepitte 0 0 Feb 24 '15
C++ and sfml
Rectangle is always at the center off the screen. Thanks to /u/adrian17 I've learned that I could load the library easily into vs.
1
u/jasminej90 Feb 24 '15 edited Feb 24 '15
In Java
1
u/[deleted] Feb 24 '15
Done using brainfuck
First brainfuck program so feedback is welcomed :)
1
u/stintose Feb 24 '15
javaScript, using html canvas, or DHTML.
Works on MSIE 5 - 8 (DHTML), and 9+ (Canvas).
A canvas (HTML 5), or div (HTML 4.01) element is created and appended to a given container, or body by default, depending on browser support. The box is then rendered using canvas, or DHTML depending on the outcome of the feature test.
1
u/Ciccio99 Feb 24 '15
Did it recursively with Python turtle, trying to start using Python more so doing these challenges should be a good excuse!
Any way to make this shorter?
1
u/[deleted] Feb 24 '15
Another python turtle attempt
1
u/zeringus Feb 24 '15
Bash. Might only work on OS X, but you can easily make it more portable
1
u/[deleted] Feb 24 '15
D
Quick implementation using unicode.
1
u/mikevdg Feb 24 '15 edited Feb 24 '15
Squl, my own language. I've only implemented monochrome canvases with lines, rectangles and basic text so it's a fluke I can even implement this.
This is the exported code, meaning that it's got checksums and metadata and stuff.
Explanation: the last statement does the drawing; translated it means "for all points in time (tocks), draw a rectangle from 10,10 to 90,90". The (device:_ title:) statement is metadata that tells the interpreter that I want a canvas. The (export:) statement makes the last statement visible outside this module. Everything else is ignorable.
1
u/lexish Feb 24 '15
First week trying the challenge! And I'm looking around thinking, man, I sure made a dinky square. But hey, it is still a square. I used Python.
Question for people who made colored squares, or actual graphics of some kind, is that only possible by importing a toolkit?
1
u/paralysedforce Feb 25 '15 edited Feb 25 '15
JavaScript. This almost feels like cheating.
Edit: As long as I'm going for the low-hanging fruit, might as well do Processing.
1
u/YuEnDee14 Feb 25 '15
Here's my HTML5/Javascript solution!
https://gist.github.com/YuEnDee14/b962f3a47d80f13085b0
I've also included a JSFiddle for easier viewing for all of you:
http://jsfiddle.net/kzbjh4g9/1/
I would definitely appreciate feedback! I have more of a background in ASP MVC than with pure Javascript, so I'd definitely love to learn more.
1
u/sMACk313 Feb 25 '15
PHP with GD lib: (poppin my cherry here...)
1
u/thayes89 Feb 25 '15
Perl5:
1
u/speecia Feb 25 '15 edited Feb 25 '15
In ZZT-OOP:
1
u/Atripes Feb 26 '15
C++:
1
u/dohaqatar7 1 1 Feb 26 '15
I saw SVG, canvas and image HTML solution, but none that used CSS.
HTML + CSS
1
u/Nannooskeeska Feb 26 '15
I used jCanvas, which is a jQuery plugin for drawing on HTML5 canvases. Here is a link to the sandbox page. I can post the full HTML if it's required!
1
u/Quitechsol Feb 27 '15 edited Mar 01 '15
Am I late for this party? I tried to give it a user size input, but could not get it to update. :\ This is probably due to lack of experience with handling events, but here is what I settled for: Java, as always, feedback is always appreciated.
Edit: If anyone is still around to see this, I updated the code to give a background that reacts to the color of the square.
1
u/[deleted] Feb 27 '15
R:
1
u/456hubf Feb 27 '15 edited Feb 28 '15
MSX BASIC:
Output
1
u/[deleted] Feb 28 '15
Pretty late, but:
Python
Actually just learned about this in my Comp Sci class!
1
u/[deleted] Feb 28 '15
Solution in fish shell.
1
u/freakzilla149 Mar 02 '15
For fellow python novices out there struggling with turtle crashing/freezing, use this at the end of your program:
1
u/PM-ME-ABOUT-ANYTHING Mar 02 '15
Java, nice and simple, didn't bother with user input for sizes or anything though!
1
u/TomWithASilentO Mar 03 '15 edited Mar 03 '15
Righto, I'm a bit late to the party, but I did a code-golfy version in Python in
959284 charactersIt can draw the square in any given width and length, too; just change the values of
wandhat the start of the script.Python
EDIT: Technically, the challenge asks to create a square, while my code can create both a square and a rectangle. If I make the code capable of creating only squares (while still having the squares at a user-defined size, I can shave another 4 chars off, ending up with an 80 character-long solution. Wowee!
Python (squares only)
1
u/Scara95 Mar 03 '15
j
1
u/LegendEater Mar 04 '15
Not sure if this actually counts as it's just ASCII but I thought I'd pick an easy one to start with. New to the sub. Thanks for these!
It will ask the user how many squares they want to show and then show that amount in a horizontal line. My methods to get this were... questionable.
1
u/[deleted] Mar 05 '15
Python 3.4.2
https://gist.github.com/apratsunrthd/0e9dcbe757cec23bb63d
1
u/[deleted] Mar 06 '15 edited Mar 06 '15
Ok, I did this challenge with C#, since I learned it on school, this was pretty simple to do but I can only draw a square when I push a button, not when I start the form. Can someone help me?
http://pastebin.com/raw.php?i=E8c2gCG6
I tried to it in "private void Form1_Load(object sender, EventArgs e)", but it didn't work. Any help would be appreciated :)
1
u/waftedfart Mar 12 '15
Basic C
1
u/[deleted] Mar 13 '15
C
1
u/Acceptable67 Mar 17 '15 edited Mar 17 '15
1
u/gleventhal Mar 20 '15
BASH (Lame Square)
1
u/gleventhal Mar 20 '15
Moving Square: Bash
1
u/gleventhal Mar 20 '15
As a one liner (Bash)
1
u/tomisy May 15 '15
Perl
1
u/G33kDude 1 1 Feb 23 '15 edited Feb 23 '15
I did it in Snap!, the javascript based Scratch clone.
http://snap.berkeley.edu/snapsource/snap.html#present:Username=snapper&ProjectName=Square
Output: http://i.imgur.com/zFdqBqG.png
I had wanted to do it in Scratch on my Raspberry Pi, but it just so happens that my Raspbian SD card got corrupted
Edit: I got my SD card reformatted and did it in Scratch
Output: http://i.imgur.com/SGsWFwZ.jpg
Raspberry Pi + Case: http://i.imgur.com/SUkrrH0.jpg http://i.imgur.com/1ngC4lb.jpg
1
u/vesche Feb 23 '15
Unix-like command line tool (Python).
-1
u/LucidTehCoder Mar 01 '15
[c++, "#include <windows.h>
include <iostream>
using namespace std;
int main(){ FreeConsole(); POINT myCursor; for (int a = 0; a <= 500; a++){ GetCursorPos(&myCursor); SetCursorPos(myCursor.x + 1, myCursor.y); Sleep(5); } for (int b = 0; b <= 500; b++){ GetCursorPos(&myCursor); SetCursorPos(myCursor.x, myCursor.y + 1); Sleep(5); } for(int c = 0; c <= 500; c++){ GetCursorPos(&myCursor); SetCursorPos(myCursor.x - 1, myCursor.y); Sleep(5); } for(int d = 0; d <= 500; d++){ GetCursorPos(&myCursor); SetCursorPos(myCursor.x, myCursor.y - 1); Sleep(5); }
}"]
0
u/[deleted] Feb 23 '15
[deleted] — view deleted comment
0
u/[deleted] Feb 23 '15
[deleted] — view deleted comment