我正在尝试制作一个循环以在开罗绘制多条线,但它在第一次迭代后停止绘制
I'm trying to make a loop to draw multiple lines in cairo but it stops drawing after the first iteration
我正在制作一个程序,人们可以在其中输入方向,在 if 语句中,它是 adds/subtracts x/y 轴,并在完成后画一条线。问题是由于某种原因,它只在第一次迭代时有效,之后不再绘制任何线条。
我添加了一个 cin >> x >> y 来测试它,但它只画了一条线并且不再画了。
最初,选择是在 switch 语句中,但我改为 if 因为我认为这是导致错误的原因。
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <cairo.h>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
char b = NULL;
char u = 'œ';
char d = 'd';
int main()
{
cairo_surface_t *surface = cairo_image_surface_create_from_png("background.png");
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 5);
double x = 0, y = 240;
cairo_move_to(cr, x, y);
long int count = 0;
int cl = 0;
int crr = 0;
int choice = 0;
int n;
system("cls");
while (choice != 5)
{
cin >> x >> y;
cairo_line_to(cr, x, y);
cairo_stroke(cr);
cairo_surface_write_to_png(surface, "spiral.png");
cout << "Current no. of points are : " << count << "/4096" << endl;
cout << "Enter direction: \n" << endl;
cout << "1 - Top Left \t 2 - Up \t 3 - Top Right " << endl;
cout << "4 - Left \t 5 - Stop \t 6 - Right" << endl;
cout << "7 - Bot. Left \t 8 - Down \t 9 - Bot. Right" << endl << endl;
cout << "Enter you choice: ";
cin >> choice;
if (choice == 1)
cout << "Test";
else
{
//More choices include the direction the person needs to go and it subtracts/adds to the x/y part
cout << "How many times ?: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
x++;
count++;
cl++;
if (cl == 256)
{
cl = 0;
crr++;
}
}
system("cls");
}
}
}
我希望它能向特定方向画线。说这个人输入正确,它向右画一条线等等。但是在这里,根本没有绘制任何线条(除非我在 while 循环的开头添加一个 cin >> x >> y,它绘制了一条线,仅此而已,没有更多的线条。)
这失败了,因为没有当前点了。在 cairo_stroke(cr);
之后,您可以添加 cairo_move_to(cr, x, y);
,它应该会按照您期望的方式开始绘制更多线条。我想......我不太确定你对这个程序有什么看法。
我正在制作一个程序,人们可以在其中输入方向,在 if 语句中,它是 adds/subtracts x/y 轴,并在完成后画一条线。问题是由于某种原因,它只在第一次迭代时有效,之后不再绘制任何线条。
我添加了一个 cin >> x >> y 来测试它,但它只画了一条线并且不再画了。 最初,选择是在 switch 语句中,但我改为 if 因为我认为这是导致错误的原因。
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <cairo.h>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
char b = NULL;
char u = 'œ';
char d = 'd';
int main()
{
cairo_surface_t *surface = cairo_image_surface_create_from_png("background.png");
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 5);
double x = 0, y = 240;
cairo_move_to(cr, x, y);
long int count = 0;
int cl = 0;
int crr = 0;
int choice = 0;
int n;
system("cls");
while (choice != 5)
{
cin >> x >> y;
cairo_line_to(cr, x, y);
cairo_stroke(cr);
cairo_surface_write_to_png(surface, "spiral.png");
cout << "Current no. of points are : " << count << "/4096" << endl;
cout << "Enter direction: \n" << endl;
cout << "1 - Top Left \t 2 - Up \t 3 - Top Right " << endl;
cout << "4 - Left \t 5 - Stop \t 6 - Right" << endl;
cout << "7 - Bot. Left \t 8 - Down \t 9 - Bot. Right" << endl << endl;
cout << "Enter you choice: ";
cin >> choice;
if (choice == 1)
cout << "Test";
else
{
//More choices include the direction the person needs to go and it subtracts/adds to the x/y part
cout << "How many times ?: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
x++;
count++;
cl++;
if (cl == 256)
{
cl = 0;
crr++;
}
}
system("cls");
}
}
}
我希望它能向特定方向画线。说这个人输入正确,它向右画一条线等等。但是在这里,根本没有绘制任何线条(除非我在 while 循环的开头添加一个 cin >> x >> y,它绘制了一条线,仅此而已,没有更多的线条。)
这失败了,因为没有当前点了。在 cairo_stroke(cr);
之后,您可以添加 cairo_move_to(cr, x, y);
,它应该会按照您期望的方式开始绘制更多线条。我想......我不太确定你对这个程序有什么看法。