Linear Cross stitch pattern generated by C ++

Source: Internet
Author: User

This boring thing can be used as a cup mat. I think I must be the best among the people with Cross stitch embroidery, haha.

I also felt that I used Java for my school project, I was looking for a job to learn JS, and finally I joined C ++. Language is just a tool! I never figured out how to draw a picture on visual stdio, So I typed it with a string.

# Include <iostream> # include <set> # include <list> # include <ctime> # include <cstdlib> # include <string> # include <windows. h> using namespace STD; // rule: Draw a picture from a point. You can start from an existing point and draw the image in the upper, lower, and left directions. However, the newly added vertex cannot touch the existing vertex. White circle on the outermost side of the canvas # define random (x) (RAND () % x) // random number # define row 50 // number of rows # define Col 50 // Number of columns # define seedx row/2 // initial position, the default value is center # define seedy COL/2int B [row] [col]; // canvas struct point {int X; int y ;}; struct compare // set sorting function {bool operator () (const point & P1, const point & p2) const {// return p1.x * Col + p1.y <p2.x * Col + p2.y; return p1.x = p2.x? P1.y <p2.y: p1.x <p2.x ;}}; void Init () // initialization, all set to 0 {for (INT I = 0; I <row; I ++) {for (Int J = 0; j <Col; j ++) {B [I] [J] = 0 ;}} void print () // print the 01 sequence of the canvas {for (INT I = 0; I <row; I ++) {for (Int J = 0; j <Col; j ++) {cout <B [I] [J] ;}cout <Endl ;}} void printas () // print the pattern in square format {string B2 [row] [col]; int COUNT = 0; For (INT I = 0; I <row; I ++) {for (Int J = 0; j <Col; j ++) {If (B [I] [J]) {B2 [I] [J] = "■ "; setconsoletextattribute (getstdhandle (std_output_handle), 0xf1 );/* Output Color description. Import windows. h and use setconsoletextattribute (getstdhandle (std_output_handle), 0x71 ). The first digit of the hexadecimal number indicates the background color, and the second digit indicates the text color, code meaning 0 = black 8 = gray 1 = blue 9 = light blue 2 = Green A = light green 3 = lake blue B = light pale green 4 = red C = light red 5 = purple d = light purple 6 = yellow E = pale yellow 7 = white F = bright white if the background is "white ", the default cmd color */count ++;} else {setconsoletextattribute (getstdhandle (std_output_handle), 0xf9); B2 [I] [J] = "□";} is displayed ";} cout <B2 [I] [J] ;}cout <count <Endl ;}} void print (set <point, compare> PL) // test the function, print the value {set <point, compare >:: iterator iter = pl. begin (); For (; it Er! = Pl. end (); ITER ++) {cout <ITER-> x <"," <ITER-> Y <"-" ;}cout <Endl ;} bool isnext (int x, int y, char DIR) // determine whether a position can draw the next {If (0 <X & Row-1> X & 0 <Y & col-1> Y) {int temp = 0; Switch (DIR) {Case 'U ': {temp = B [x-1] [Y-1] + B [x-1] [Y] + B [x-1] [Y + 1] + B [x] [Y-1] + B [x] [Y] + B [x] [Y + 1]; break;} case 'D ': {temp = B [x] [Y-1] + B [x] [Y] + B [x] [Y + 1] + B [x + 1] [Y-1] + B [x + 1] [Y] + B [x + 1] [Y + 1]; break;} case 'l': {temp = B [x-1] [Y-1] + B [x-1] [Y] + B [x] [y- 1] + B [x] [Y] + B [x + 1] [Y-1] + B [x + 1] [Y]; break;} case 'r ': {temp = B [x-1] [Y] + B [x-1] [Y + 1] + B [x] [Y] + B [x] [Y + 1] + B [x + 1] [Y] + B [x + 1] [Y + 1]; break ;}}if (temp = 0) return true;} return false;} void findandase (point P, set <point, compare> & nextset) // after a new vertex is added, delete the vertex {set <point, compare >:: iterator itererase; itererase = nextset. find (p); If (itererase! = Nextset. end () {nextset. erase (itererase) ;}} void setnextset (point seed, set <point, compare> & nextset) // set the candidate list {int x = seed. x; // X indicates the row, and y indicates the column int y = seed. y; point P; p. X = X-1; p. y = y; If (isnext (x-1, Y, 'U') {nextset. insert (p);} else {findandrease (p, nextset);} p. X = x + 1; p. y = y; If (isnext (x + 1, Y, 'd) {nextset. insert (p);} else {findandrease (p, nextset);} p. X = x; p. y = Y-1; If (isnext (x, Y-1, 'L') {nextset. insert (p);} else {fi Ndandase (p, nextset);} p. X = x; p. y = Y + 1; if (isnext (X, Y + 1, 'R') {nextset. insert (p);} else {findandrease (p, nextset);} p. X = X-1; p. y = Y-1; findandase (p, nextset); // corner adjacent to P. X = x + 1; p. y = Y-1; findandase (p, nextset); p. X = X-1; p. y = Y + 1; findandrease (p, nextset); p. X = x + 1; p. y = Y + 1; findandrease (p, nextset);} // obtain the next position through a random number. Seed generate set <point, compare >:: iterator getnextiter (set <point, compare> & nextset) {int COUNT = nextset. size (); set <point, compare >:: iterator iternext = nextset. begin (); int Pos = int (random (count); For (INT I = 0; I <Pos; I ++) {iternext ++;} return iternext ;} // draw the next one: select one from the candidate, change the value of B, and determine the candidates for the four positive directions. If the candidate is added, it is not a candidate to search for and delete; determine the four oblique directions and delete them from the candidate list. Void darwnext (set <point, compare> & nextset) {set <point, compare >:: iterator iternext; iternext = getnextiter (nextset); point next; next. X = iternext-> X; next. y = iternext-> Y; B [next. x] [next. y] = 1; nextset. erase (iternext); setnextset (next, nextset);} void generate () // use the default seed to generate {B [seedx] [seedy] = 1; set <point, compare> nextset; point seed; seed. X = seedx; seed. y = seedy; setnextset (seed, nextset); While (! Nextset. empty () {darwnext (nextset) ;}} void main () {Init (); srand (unsigned (time (0); generate (); printas ();}

The effects of the image are different !)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.