I am working a program to create a Koch curve using a main() with a recursive function that outputs postscript cout statements. At time of execution, we do output redirection to a .ps file to view the curve. How do I calculate the coordinates given beginning and end points (x1,y1) & (x2,y2)? We are using c++11 so I can’t import turtle or any other graphics library, nor should I need too.
![pe Acrobat Reader DC (64-bit)
Help
ogram3Descripti. x
3 /3
75%
left corner. A simple postscript program that draws a level 1 snowflake is (generated by koch 72
360 504 360 1):
%!PS-Adobe-2.0
72 360 moveto
144
O rlineto
72
125 rlineto
72 -125 rlineto
144
O rlineto
stroke
showpage
Statement of Work
Write a program (yes, that means an entire program, including a .cpp file with main()) to draw a
Koch curve of arbitrary level and with arbitrary starting line segment endpoints using a recursive
algorithm. Your program should take the initial line segment endpoints and the desired final level
as command line arguments. So, if your executable is named "koch" (on Linux/MacOS) or
"koch.exe" (on Windows), you would run it as:
koch x1 y1 x2 y2 level
The first four arguments are the starting line segment endpoints (#1, Y1) and (*2, Y2) in page
coordinates; the last is the curve level (>0).
Your program should send to cout a Postscript program (which is plain text) to draw the
requested curve. If you save this to a file with an extension of .ps (using output redirection, for
example with > on the command line), you can view the result on MacOS and Linux by double-
clicking on the .ps file (for Windows, you may need to download Ghostscript
(https://www.ghostscript.com/)_to preview Postscript files). You can also preview Postscript files
Tisina Gooale Drive by savina the file to Drive and then iust double-clicking there
hp](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F7c3099fd-5279-4e87-a750-c1438fd02c89%2Faeb8838c-a371-41d0-81c0-c43eafc0dc34%2Fkv6atc_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Code for Koch Curve in C++
#include <bits/stdc++.h>
#include<iostream>
#include<math.h>
using namespace std;
void k_curve(long double,long double,long double,long double,int);
void line1(int,int,int,int);
int main()
{
int gd; int gm;
long double x=0;
long double y=0;
long double l=0;
long double angle=0;
int n=0;
cout<<"Starting point of the line:(x,y):"<<endl;
cout<<"_________________________________"<<endl;
cout<<"Enter the value of x="<<endl;
cin>>x;
cout<<"Enter the value of y="<<endl;
cin>>y;
cout<<"Enter the value of line:l:"<<endl;
cin>>l;
cout<<"Angle of the line with x axis:"<<endl;
cin>>angle;
cout<<"Enter the order of curve=n="<<endl;
cin>>n;
initgraph(&gd,&gm,"");
k_curve(x,y,l,angle,n);
closegraph();
return 0;
}
void k_curve(long double x,long double y,long double lenght,long double angle,int
n_order)
{ if(n_order>0)
{
lenght/=3;
k_curve(x,y,lenght,angle,(n_order-1));
x+=(lenght*cosl(angle*(M_PI/180)));
y+=(lenght*sinl(angle*(M_PI/180)));
k_curve(x,y,lenght,(angle-60),(n_order-1));
x+=(lenght*cosl((angle-60)*(M_PI/180)));
y+=(lenght*sinl((angle-60)*(M_PI/180)));
k_curve(x,y,lenght,(angle+60),(n_order-1));
x+=(lenght*cosl((angle+60)*(M_PI/180)));
y+=(lenght*sinl((angle+60)*(M_PI/180)));
k_curve(x,y,lenght,angle,(n_order-1));
}
else
line1(x,y,(int)(x+lenght*cosl(angle*(M_PI/180))+0.5),(int)(y+lenght*sinl(angle*(M_PI
/180))));
}
void line1(int x_1,int y_1, int x_2,int y_2)
{
int color=getcolor();
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);put
while(y!=y2)
{ y+=inc_dec;
if(p<0)
p+=two_dx;
else
{x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
} }}
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)