我正在尝试制作一个循环来在cairo中绘制多条线,但它在第一次迭代后停止绘制

问题描述 投票:0回答:1

我正在创建一个人可以输入方向的程序,在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
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.