首页 > 解决方案 > 我正在尝试制作一个循环以在 cairo 中绘制多条线,但它在第一次迭代后停止绘制

问题描述

我正在制作一个程序,一个人可以输入一个方向,在 if 语句中,它添加/减去 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 ,这会画一条线,就是这样,没有更多的线。)

标签: visual-c++cairo

解决方案


这失败了,因为没有当前点了。之后cairo_stroke(cr);,您可以添加cairo_move_to(cr, x, y);,它应该开始以您期望的方式绘制更多线条。我想...我不太确定你对这个程序有什么用。


推荐阅读