Recursive decomposition cun also be used lo draw a stylized representation of a
ID: 3632987 • Letter: R
Question
Recursive decomposition cun also be used lo draw a stylized representation of a tree. the tree begins as a simple trunk indicated by a straight vertical line. The trunk may branch at the top to form two lines veer off at an angle, as shown in the figure on page 234. These branches may themselves split lo form new branches, which split to form new ones, and so on. If the decision to branch is made randomly at each step of the process, the tree will eventually become unsymmetrical and will end up looking a little more like trees in nature, as illustrated by the diagram on page 234. If you think about this process recursively, however, you can see that all trees constructed in this way consist of a trunk, optionally topped by two trees that veer off at an angle. If the probability of branching is a function of the length of the current branch, the process will eventually terminate as the branches get progressively shorter. Write a program draw reo. cop hall uses this recursive strategy and the graphic library to draw a stylized line drawing of a tree.Explanation / Answer
#include "genlib.h"
#include "graphics.h"
#include "random.h"
/*
* Constants
* ---------
* MIN_AREA -- Smallest square that will be split
* MIN_EDGE -- Minimum fraction on each side of dividing line
*/
const double MIN_AREA = 0.5;
const double MIN_EDGE = 0.15;
/* Private function prototypes */
void SubdivideCanvas(double x, double y, double width, double height);
/* Main program */
int main() {
InitGraphics();
Randomize();
SubdivideCanvas(0, 0, GetWindowWidth(), GetWindowHeight());
return 0;
}
void SubdivideCanvas(double x, double y, double width, double height) {
if (width * height >= MIN_AREA) {
if (width > height) {
double mid = width * RandomReal(MIN_EDGE, 1 - MIN_EDGE);
MovePen(x + mid, y);
DrawLine(0, height);
SubdivideCanvas(x, y, mid, height);
SubdivideCanvas(x + mid, y, width - mid, height);
} else {
double mid = height * RandomReal(MIN_EDGE, 1 - MIN_EDGE);
MovePen(x, y + mid);
DrawLine(width, 0);
SubdivideCanvas(x, y, width, mid);
SubdivideCanvas(x, y + mid, width, height - mid);
}
}
}
or
#include <string>
#include <time.h>
#include "arg_utils_ext.hpp"
#include "file_utils.hpp"
#include <iostream>
#include <fstream>
#include "SequenceTree.hpp"
using namespace std;
void
print_options(char *note = NULL){
if ( note != NULL ){
cout << "ERROR: " << note <<endl<< endl;
}
cout <<
"OPTIONS MAN/DEF DESCRIPTION "
" -seqs file A sequence file. "
" -d int 1 Number of datasets in file. "
" -otree file A file to print the phylip tree with internal node names set. "
" -seqgen Set the names of internal nodes as seqgen would "
" -h or --help Print this help message "
<< endl;
cout << "EXAMPLE USAGE" << endl;
cout << " $./drawtree treefile.txt -seqs infile.seq" <<endl;
exit(1);
}
int
main( int argc, char ** argv){
TRY_EXCEPTION();
if ( HAS_OPTION("-h") || HAS_OPTION("--help") )
print_options();
//--------
if ( ! file_exists(argv[1]) )
print_options("tree input file doesn't exist");
ifstream inTreeFile;
open_read_stream(argv[1], inTreeFile);
//------
bool seqgen = GET_BOOLEAN_OPTION_VAL("-seqgen");
//--------------
SequenceTree resultTree;
SequenceTree::NodeVector nodes;
//files
vector<Sequence> seqs;
int numdatasets = 1;
char *dstr = GET_OPTION_VAL("-d");
if ( dstr != NULL ) numdatasets = atoi(dstr);
char *seqfile = GET_OPTION_VAL("-seqs");
ifstream inSeqFile;
if (seqfile != NULL )
open_read_stream(seqfile,inSeqFile);
char *otreefile = GET_OPTION_VAL("-otree");
ofstream outTreeFile;
if ( otreefile != NULL )
open_write_stream(otreefile,outTreeFile);
for ( int i = 0 ; i < numdatasets ; i++ ){
cout << "**************"<< endl;
cout << "Dataset " << (i+1) << endl;
//read the tree
resultTree = SequenceTree(inTreeFile);
if ( seqgen ){
nodes.clear();
resultTree.addNodesInPrefixOrder(nodes);
int nodename = resultTree.getNumLeafs() + 1;
for ( size_t i = 0 ; i < nodes.size() ; i++ ){
if ( nodes[i]->isLeaf() ) continue;
nodes[i]->data.s.name = string("")+nodename;
nodename++;
}
}
else{
resultTree.setNodeNames();
}
if ( seqfile != NULL ) {
resultTree.mapSequencesOntoTree(inSeqFile);
}
resultTree.drawTree(cout);
if ( seqfile != NULL )
resultTree.printSequences(cout);
if ( otreefile != NULL )
outTreeFile << resultTree << endl;
}
cout << "**************"<< endl;
CATCH_EXCEPTION();
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.