Showing posts with label 2D computer graphics. Show all posts
Showing posts with label 2D computer graphics. Show all posts

Tuesday, June 23, 2015

L-Systems | Sierpinski Triangle in VB.NET


The Sierpinski triangle (also with the original orthography SierpiƄski), also called the Sierpinski gasket or the Sierpinski Sieve, is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles.

Public Class Form1
   'We have used the following rules
   ' -1 = turn right by angle
   ' +1 = turn left by angle
   ' 2 = 'a'
   ' 3 = 'b'
   'Therefore te rules are represented like this
   ' 2 --> 3 -1 2 1 3
   ' 3 --> 2 +1 3 +1 2
   'Start Symbol : a
   ' 
   Dim angle As Double
   Dim size_of_triangle As Integer
   Dim no_of_iter As Integer
   Dim t As Integer
   Dim lst As List(Of Integer) = New List(Of Integer)

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       numIter.Value = 7
       numSize.Value = 4
       numAngle.Value = 60
   End Sub

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       Dim drawingPt As Point = New Point(250, Me.ClientSize.Height - 5)
       Dim D As Double = 0
       Dim P1 As Point
       For Each i As Integer In lst
           Select Case i
               Case -1
                   D -= angle
               Case 1
                   D += angle
               Case 2, 3
                   P1 = getNewPoint(drawingPt, D)
                   e.Graphics.DrawLine(Pens.Tomato, drawingPt, P1)
                   drawingPt = P1
           End Select
       Next

   End Sub

   Private Function getNewPoint(ByVal p0 As Point, ByVal d As Double) As Point
       Dim p1 As Point
       p1.X = p0.X + Math.Cos(d) * size_of_triangle
       p1.Y = p0.Y + Math.Sin(d) * size_of_triangle
       Return p1
   End Function

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Label4.Visible = True
       Me.TransparencyKey = Color.Beige
       angle = (numAngle.Value) * Math.PI / 180
       size_of_triangle = numSize.Value
       no_of_iter = numIter.Value
       t = 1
       If (no_of_iter Mod 2) = 1 Then t = -1
       lst = New List(Of Integer)
       lst.Add(2)
       For i As Integer = 0 To no_of_iter
           lst = Expand(lst)
       Next i
       Me.Invalidate()
   End Sub

   Private Function Expand(ByVal lst As List(Of Integer)) As List(Of Integer)
       Dim lst1 As List(Of Integer) = New List(Of Integer)
       For Each i As Integer In lst
           Select Case i
               Case -1, 1
                   lst1.Add(i)
               Case 2
                   ' 2 --> 3 -1 2 -1 3
                   lst1.Add(3)
                   lst1.Add(-1 * t)
                   lst1.Add(2)
                   lst1.Add(-1 * t)
                   lst1.Add(3)
               Case 3
                   ' 3 --> 2 +1 3 +1 2
                   lst1.Add(2)
                   lst1.Add(1 * t)
                   lst1.Add(3)
                   lst1.Add(1 * t)
                   lst1.Add(2)
           End Select
       Next
       Return lst1
   End Function

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       Application.Exit()
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Label4.Visible = False
       Me.TransparencyKey = Color.Black
   End Sub
End Class



Related articles
Continue Reading →

Tuesday, June 16, 2015

Visual Stack Application in Java

I made an application to graphically demonstrate the working of Stack. So, I made this application called Visual Stack which allows the users to interact and understand the working of Stack. In this post I have posted about VisualStack. Read ahead to get to know more about it.

Stack
Stack is a data structure that follows the LIFO (Last-In-First-Out) principle. Newly added elements are added to the top and the most recent inserted data are to be removed first as well.

Visual Stack
This applet is very simple and shows the working of a stack.

Video Demonstration


Screenshot

[Image: visualstack_zps5e183d58.png]

Description
We have a text area and you need to write certain commands for this to work. We need to know about the stack ADT first. A stack does the following operations :-

1. Push ; Pushes an element to the top.
2. Pop: Removes the top element
3. peek: Returns the top element.
4. size: returns the size of the stack.

We have 5 keywords here, namely - push, pop, top, size, and delay.

push and delay work as the following syntax :- 
<instruction> <single-space> <data>

push :- Push the data into the stack. Example : push you, push 2, push IAmDull etc
delays : delay the update of the stack and its visualization.

pop, top, size are single instructions.

pop : pops the top element.
top :
Shows the top element in logs.
size : Shows the size of the stack in logs.

Project on Github :- https://github.com/rawCoders/VisualStack


Related articles
Continue Reading →

Lorenz System Atrractor - Chaotic Nature of Differential Equations

Lorenz attractor
Lorenz attractor (Photo credit: Wikipedia)
The Lorenz system is a system of ordinary differential equations (the Lorenz equations, note it is not Lorentz) first studied by Edward Lorenz. It is notable for having chaotic solutions for certain parameter values and initial conditions. In particular, the Lorenz attractor is a set of chaotic solutions of the Lorenz system which, when plotted, resemble a butterfly or figure eight.


Code With Live Demo

Continue Reading →

Monday, June 15, 2015

Image to ASCII ART converter in Java

English: ascii art example. "Block" ...
English: ascii art example. "Block" or "High ASCII" style, cf. ANSI art. (Photo credit: Wikipedia)

ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII). The term is also loosely used to refer to text based visual art in general.
-- From Wikipedia

Ascii Art is pretty cool and there are several online services available which allow you to use convert an image to its ASCII ART form. These are often used in image boards or online communities as Avatar images. So I made a little java program which takes an image and converts it to its ASCII form.

How to Use ?

This code doesn't takes command line input so you need to pass the image name with extension in the main function.

Example :-

obj.convertToAscii("Absolute_Path_To_Your_Image.jpg");

When you run the code a text file "asciiart.txt" is created which has the Ascii Art of the image you gave as input. For better vision of the converted image set the font size to 2 or 1. 
Source Code
Sample Run :-

The image I used is :-

[Image: 2YpkRjy.png]

The output is (See the text in notepad)

[Image: i0J2jdp.png]
Sample Demo 2
Input Image
 
Output
I hope you learned something new and will be using this code to make your own ASCII art from images. See you later :) 
Continue Reading →

Follow Me!

Followers

Visitor Map