SlideShare uma empresa Scribd logo
1 de 1
Baixar para ler offline
The Joy of
Programming
Writing Beautiful Programs: ASCII
Arts and Fractals—Part II                                                                                                  S.G. GANESH


Computer programs can be written for aesthetic purposes also. This column gives an introduction to
the basics of computer art using C, and is meant for students and novice programmers.



T
          he term ‘fractal’ was coined in 1975 by a                      @ . @ . @ . @ . . . . . . . . . @ . @ . @ . @
          mathematician named Mandelbrot. Fractals are                   @ @ . . @ @ . . . . . . . . . . @ @ . . @ @
          created based on some simple patterns and simple               @ . . . @ . . . . . . . . . . . @ . . . @
rules. An important characteristic of fractals is that they are          @ @ @ @ . . . . . . . . . . . . @ @ @ @
‘self-similar’—a small portion of a fractal, when magnified, can         @ . @ . . . . . . . . . . . . . @ . @
reproduce a larger portion of the fractal!                               @ @ . . . . . . . . . . . . . . @ @
     It is easy to write computer programs to create fractals,           @ . . . . . . . . . . . . . . . @
since fractals are produced with simple mathematical rules and           @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
are based on recursion.                                                  @ . @ . @ . @ . @ . @ . @ . @
     In this column, we’ll create a variation of a simple, well-         @ @ . . @ @ . . @ @ . . @ @
known fractal called the ‘Sierpinski triangle fractal’.                  @ . . . @ . . . @ . . . @
     Writing portable C programs to create graphical                     @ @ @ @ . . . . @ @ @ @
representation of fractals is not possible because the standard          @ . @ . . . . . @ . @
C language does not define any functions for graphics                    @ @ . . . . . . @ @
programming. It is possible to write graphics programs that use          @ . . . . . . . @
the facilities provided by the underlying operating system;              @ @ @ @ @ @ @ @
however, for now, we’ll limit ourselves to creating fractals with        @ . @ . @ . @
ASCII characters for portability.                                        @ @ . . @ @
                                                                         @ . . . @
 int main(){                                                             @ @ @ @
 /* the image is of 2 * MAX * MAX characters */                          @ . @
     const int MAX = 32;                                                 @ @
     int col    = 0, row = 0;                                            @
     do {
        if(col >= row)                                                       For such a small program, the result is quite interesting,
               printf(“ %c”, (~col & row) ? ‘.’ : ‘@’);                 isn’t it? Let’s see how it works.
        col++;                                                               The two variables, row and col, are to track the rows and
        if (col >= MAX) {                                               columns for printing the characters. After printing MAX
               col = 0;   // reset col to start again                   characters for each row, we reset the col count to zero to start
               row++; // go to next line                                afresh in the next row; so we increment the row count. To get a
               printf(“n”);                                            new row in the picture, we print a newline character.
        }                                                                    The crux of the program is the expression “(~col & row)”—if
     } while (row != MAX);                                              it becomes true, we print the ‘.’ character, else we print the ‘@’
 }                                                                      character. You can use any two different looking characters for
                                                                        output (say, the ‘`’ and ‘#’ characters). To understand how the
    When you execute this program, you’ll get the following             expression “(~col & row)” works, print the values of ~col, row,
picture:                                                                and (~col & row) for each iteration and check the results (or
                                                                        mentally do the calculation for given values of col and row).
 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @             Note that fractal programs typically use the formal
 @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @          mathematical model and/or use recursion to create the fractal.
 @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @            In this column, to introduce fractals, we use the power of C
 @ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @              bitwise operators to get the desired result. However, not all
 @ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @                fractals can be written this way.
 @ . @ . . . . . @ . @ . . . . . @ . @ . . . . . @ . @
 @ @ . . . . . . @ @ . . . . . . @ @ . . . . . . @ @                     By: S.G. Ganesh is a research engineer at Siemens
 @ . . . . . . . @ . . . . . . . @ . . . . . . . @                       (corporate technology) in Bangalore. You can reach him at
 @ @ @ @ @ @ @ @ . . . . . . . . @ @ @ @ @ @ @ @                         sgganesh@gmail.com

                                                                               www.linuxforu.com   |   LINUX FOR YOU   |   AUGUST 2007   91


                                                                 CMYK

Mais conteúdo relacionado

Semelhante a 08 Jo P Aug 07

Maxima - minimalism in mathematics
Maxima - minimalism in mathematicsMaxima - minimalism in mathematics
Maxima - minimalism in mathematicsSergeiPronkevich
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Ganesh Samarthyam
 
Macro expansion techinical_report
Macro expansion techinical_reportMacro expansion techinical_report
Macro expansion techinical_reportSandun Perera
 
Console I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptxConsole I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptxPRASENJITMORE2
 
20090814102834_嵌入式C与C++语言精华文章集锦.docx
20090814102834_嵌入式C与C++语言精华文章集锦.docx20090814102834_嵌入式C与C++语言精华文章集锦.docx
20090814102834_嵌入式C与C++语言精华文章集锦.docxMostafaParvin1
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
A Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and AllegroA Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and Allegrosnowfarthing
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2Philip Schwarz
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++Neeru Mittal
 

Semelhante a 08 Jo P Aug 07 (20)

01 Jo P Jan 07
01 Jo P Jan 0701 Jo P Jan 07
01 Jo P Jan 07
 
Maxima - minimalism in mathematics
Maxima - minimalism in mathematicsMaxima - minimalism in mathematics
Maxima - minimalism in mathematics
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
 
Pascal programming lecture notes
Pascal programming lecture notesPascal programming lecture notes
Pascal programming lecture notes
 
03 Jo P Mar 07
03 Jo P Mar 0703 Jo P Mar 07
03 Jo P Mar 07
 
14 Jo P Feb 08
14 Jo P Feb 0814 Jo P Feb 08
14 Jo P Feb 08
 
Macro expansion techinical_report
Macro expansion techinical_reportMacro expansion techinical_report
Macro expansion techinical_report
 
Console I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptxConsole I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptx
 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
 
C Programming
C ProgrammingC Programming
C Programming
 
20090814102834_嵌入式C与C++语言精华文章集锦.docx
20090814102834_嵌入式C与C++语言精华文章集锦.docx20090814102834_嵌入式C与C++语言精华文章集锦.docx
20090814102834_嵌入式C与C++语言精华文章集锦.docx
 
Bioinformatica p4-io
Bioinformatica p4-ioBioinformatica p4-io
Bioinformatica p4-io
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
A Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and AllegroA Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and Allegro
 
Go1
Go1Go1
Go1
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
Prototype js
Prototype jsPrototype js
Prototype js
 

Mais de Ganesh Samarthyam

Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeGanesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGanesh Samarthyam
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionGanesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationGanesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterGanesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckGanesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageGanesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Ganesh Samarthyam
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz QuestionsGanesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizGanesh Samarthyam
 

Mais de Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 

Último

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

08 Jo P Aug 07

  • 1. The Joy of Programming Writing Beautiful Programs: ASCII Arts and Fractals—Part II S.G. GANESH Computer programs can be written for aesthetic purposes also. This column gives an introduction to the basics of computer art using C, and is meant for students and novice programmers. T he term ‘fractal’ was coined in 1975 by a @ . @ . @ . @ . . . . . . . . . @ . @ . @ . @ mathematician named Mandelbrot. Fractals are @ @ . . @ @ . . . . . . . . . . @ @ . . @ @ created based on some simple patterns and simple @ . . . @ . . . . . . . . . . . @ . . . @ rules. An important characteristic of fractals is that they are @ @ @ @ . . . . . . . . . . . . @ @ @ @ ‘self-similar’—a small portion of a fractal, when magnified, can @ . @ . . . . . . . . . . . . . @ . @ reproduce a larger portion of the fractal! @ @ . . . . . . . . . . . . . . @ @ It is easy to write computer programs to create fractals, @ . . . . . . . . . . . . . . . @ since fractals are produced with simple mathematical rules and @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ are based on recursion. @ . @ . @ . @ . @ . @ . @ . @ In this column, we’ll create a variation of a simple, well- @ @ . . @ @ . . @ @ . . @ @ known fractal called the ‘Sierpinski triangle fractal’. @ . . . @ . . . @ . . . @ Writing portable C programs to create graphical @ @ @ @ . . . . @ @ @ @ representation of fractals is not possible because the standard @ . @ . . . . . @ . @ C language does not define any functions for graphics @ @ . . . . . . @ @ programming. It is possible to write graphics programs that use @ . . . . . . . @ the facilities provided by the underlying operating system; @ @ @ @ @ @ @ @ however, for now, we’ll limit ourselves to creating fractals with @ . @ . @ . @ ASCII characters for portability. @ @ . . @ @ @ . . . @ int main(){ @ @ @ @ /* the image is of 2 * MAX * MAX characters */ @ . @ const int MAX = 32; @ @ int col = 0, row = 0; @ do { if(col >= row) For such a small program, the result is quite interesting, printf(“ %c”, (~col & row) ? ‘.’ : ‘@’); isn’t it? Let’s see how it works. col++; The two variables, row and col, are to track the rows and if (col >= MAX) { columns for printing the characters. After printing MAX col = 0; // reset col to start again characters for each row, we reset the col count to zero to start row++; // go to next line afresh in the next row; so we increment the row count. To get a printf(“n”); new row in the picture, we print a newline character. } The crux of the program is the expression “(~col & row)”—if } while (row != MAX); it becomes true, we print the ‘.’ character, else we print the ‘@’ } character. You can use any two different looking characters for output (say, the ‘`’ and ‘#’ characters). To understand how the When you execute this program, you’ll get the following expression “(~col & row)” works, print the values of ~col, row, picture: and (~col & row) for each iteration and check the results (or mentally do the calculation for given values of col and row). @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ Note that fractal programs typically use the formal @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ . @ mathematical model and/or use recursion to create the fractal. @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ . . @ @ In this column, to introduce fractals, we use the power of C @ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @ . . . @ bitwise operators to get the desired result. However, not all @ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @ . . . . @ @ @ @ fractals can be written this way. @ . @ . . . . . @ . @ . . . . . @ . @ . . . . . @ . @ @ @ . . . . . . @ @ . . . . . . @ @ . . . . . . @ @ By: S.G. Ganesh is a research engineer at Siemens @ . . . . . . . @ . . . . . . . @ . . . . . . . @ (corporate technology) in Bangalore. You can reach him at @ @ @ @ @ @ @ @ . . . . . . . . @ @ @ @ @ @ @ @ sgganesh@gmail.com www.linuxforu.com | LINUX FOR YOU | AUGUST 2007 91 CMYK