Omnimaga
General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: Juju on April 26, 2013, 06:01:32 pm
-
Post random interesting PHP, Python, Javascript or whatever piece of code here.
<?php
function uchr($u){return mb_convert_encoding('&#'.intval($u).';','UTF-8','HTML-ENTITIES');}
$text = trim($argv[1],"\0..\x2f\x3a..\x40\x47..\x60\x67..\xff");
if(strlen($text)%2==1)$text="0".$text;
if(strlen($text)%4==2)$text.="00";
$char = Array(uchr(0x2000),uchr(0x2597),uchr(0x2596),uchr(0x2584),
uchr(0x259D),uchr(0x2590),uchr(0x259E),uchr(0x259F),
uchr(0x2598),uchr(0x259A),uchr(0x258C),uchr(0x2599),
uchr(0x2580),uchr(0x259C),uchr(0x259B),uchr(0x2588));
for($i=0;$i<strlen($text);$i+=4){
$byte = hexdec(substr($text,$i,2));
$byte2 = hexdec(substr($text,$i+2,2));
echo $char[(($byte&192)>>4)|(($byte2&192)>>6)].$char[(($byte&48)>>2)|(($byte2&48)>>4)];
echo $char[($byte&12)|(($byte2&12)>>2)].$char[(($byte&3)<<2)|($byte2&3)]."\n";
}
Input: An 8x8 Axe sprite
Output: The sprite drawn using 4x4 characters.
Example:
[julien@haruhi ~]$ php axesprite.php [AA55AA55aa55aa55]
▚▚▚▚
▚▚▚▚
▚▚▚▚
▚▚▚▚
Why it's interesting: It uses bitwise operations! Also 4 pixels in a single character.
EDIT: Ignore the & at line 2 it's supposed to be a &.
-
Lol, nice one. Will post one myself soon.
-
<?php
ob_start();
echo $num="1"," ";
$c=0;
for($a=0;$a<$argv[1]-1;$a++)
{
$newnum="";
for($i=0;$i<strlen($num);$i++)
{
$c++;
if($num[$i] != (isset($num[$i+1])?$num[$i+1]:""))
{
$newnum .= $c.$num[$i];
$c=0;
}
}
echo $num=$newnum," ";
}
$out = ob_get_contents();
ob_end_clean();
echo str_replace(Array("1", "2", "3", " "),
Array(chr(27)."[44m1",
chr(27)."[42m2",
chr(27)."[41m3",
chr(27)."[0m "),
chunk_split($out, trim(`tput cols`), chr(27)."[0m\n"));
This one, running from CLI, makes use of output buffers to add some pretty colors :) Takes one parameter, its the number of numbers to generate.
Example:
(http://img.removedfromgame.com/imgs/woolotsofcolors.png)
-
Ahhhh... My eyes are burning... *.* Colors!
-
...
Input: An 8x8 Axe sprite
Output: The sprite drawn using 4x4 characters.
Example:
[julien@haruhi ~]$ php axesprite.php [AA55AA55aa55aa55]
▚▚▚▚
▚▚▚▚
▚▚▚▚
▚▚▚▚
Why it's interesting: It uses bitwise operations! Also 4 pixels in a single character.
EDIT: Ignore the & at line 2 it's supposed to be a &.
The 'sprite' doesn't look like a sprite to me. It just looks like 16 squares, in a square.
-
...
Input: An 8x8 Axe sprite
Output: The sprite drawn using 4x4 characters.
Example:
[julien@haruhi ~]$ php axesprite.php [AA55AA55aa55aa55]
▚▚▚▚
▚▚▚▚
▚▚▚▚
▚▚▚▚
Why it's interesting: It uses bitwise operations! Also 4 pixels in a single character.
EDIT: Ignore the & at line 2 it's supposed to be a &.
The 'sprite' doesn't look like a sprite to me. It just looks like 16 squares, in a square.
You must lack the necessary fonts to show this character up. Which OS are you using? If Linux, try installing the Microsoft fonts, probably.
-
I'm using windows 7.
This is a screenshot of how it shows up to me:
(http://img.removedfromgame.com/imgs/23-Untitled.png)
-
I'm also using Windows 7, but for me it shows up more or less correctly:
(http://img.removedfromgame.com/imgs/sprite.PNG)
-
Here is something I did in my spare time in C#. It's not really a snippit, but fun anyway^^
Main.cs - Main program code
using System;
namespace Fractal
{
class MainClass
{
public static void TriLine(double distance, Turtle myTurtle)
{
Console.ForegroundColor = ConsoleColor.Magenta;
if (distance < 1)
{
myTurtle.Forward(distance);
}
else
{
TriLine (distance/3, myTurtle);
myTurtle.Left (60);
TriLine (distance/3, myTurtle);
myTurtle.Right (120);
TriLine (distance/3, myTurtle);
myTurtle.Left (60);
TriLine (distance/3, myTurtle);
}
}
public static void Main (string[] args)
{
Console.WriteLine ("Press any key to start");
Console.ReadLine ();
Display myDisplay = new Display ();
Turtle myTurtle = new Turtle (myDisplay);
//myTurtle.HomeXPos = 10;
myTurtle.HomeYPos = 10;
for (int i = 0; i < 3; i++) {
TriLine(300, myTurtle);
myTurtle.Right(120);
}
myDisplay.UpdateDisplay ();
}
}
}
Turtle.cs - Turtle 'engine'
using System;
namespace Fractal
{
public class Turtle
{
public double XPos {get; set;}
public double YPos {get; set;}
public double Angle {get; set;}
public bool Pen {get; set;}
public double HomeXPos {get; set;}
public double HomeYPos {get; set;}
private Display myDisplay;
public Turtle(Display myDisplay)
{
XPos = 0;
YPos = 0;
Angle = 0;
Pen = true;
this.myDisplay = myDisplay;
HomeXPos = myDisplay.Width / 2;
HomeYPos = myDisplay.Height / 2;
}
public static double DegToRad(double deg)
{
return deg * Math.PI / 180;
}
public void Forward(double distance)
{
double oldX = XPos;
double oldY = YPos;
double angle = DegToRad(this.Angle);
YPos = oldY + distance * Math.Cos(angle);
XPos = oldX + distance * Math.Sin(angle);
if (Pen)
{
myDisplay.DrawLine (
(int) Math.Round(HomeYPos + oldY),
(int) Math.Round(HomeXPos + oldX),
(int) Math.Round(HomeYPos + YPos),
(int) Math.Round(HomeXPos + XPos)
);
//m
}
}
public void Left(double angleDelta)
{
Angle -= angleDelta;
}
public void Right(double angleDelta)
{
Angle += angleDelta;
}
public override string ToString ()
{
return string.Format ("[Turtle: XPos={0}, YPos={1}, Angle={2}]", XPos, YPos, Angle);
}
}
}
Display.cs - Allows you to draw to your console
using System;
namespace Fractal
{
public class Display
{
public const string PIXEL_ON = "\u2588\u2588";
public const string PIXEL_OFF = " ";
public const int PIXEL_CHARS = 2;
public int Height { get; set; }
public int Width { get; set; }
public bool[,] DisplayBuffer;
public Display()
{
SetDisplay ();
}
public void SetDisplay ()
{
Width = Console.BufferWidth / PIXEL_CHARS;
Height = Console.BufferHeight - 2;
DisplayBuffer = new bool[Height, Width];
for (int i = 0; i < DisplayBuffer.Length; i++) {
DisplayBuffer [(int) i/Width, i%Width] = false;
}
}
public void SetPixel (int y, int x, bool state)
{
if (x > 0 && y > 0 && x < Width && y < Height)
DisplayBuffer [y, x] = state;
}
public void DrawLine(int x1, int y1, int x2, int y2) {
int w = x2 - x1;
int h = y2 - y1;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
int longest = Math.Abs(w) ;
int shortest = Math.Abs(h) ;
if (!(longest>shortest)) {
longest = Math.Abs(h) ;
shortest = Math.Abs(w) ;
if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
dx2 = 0 ;
}
int numerator = longest >> 1;
for (int i=0; i<=longest; i++) {
SetPixel(x1 , y1, true) ;
numerator += shortest ;
if (!(numerator<longest)) {
numerator -= longest ;
x1 += dx1 ;
y1 += dy1 ;
} else {
x1 += dx2 ;
y1 += dy2 ;
}
}
}
public void UpdateDisplay()
{
Console.SetCursorPosition (0, 0);
for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
string pixel = DisplayBuffer[y, x] ? PIXEL_ON : PIXEL_OFF;
Console.Write (pixel);
}
Console.WriteLine ();
}
}
}
}
Now, here is what it looks like:
(http://i.imgur.com/happYML.png)
Of course, I set my terminal to have very very many rows and columns so I can simulate pixels. (however you can change the code to work on a 'lower' resolution)