01. เขียนรูปกราฟ 2
ตัวอย่างที่ 1.1 เขียนเส้นตรง วงรี และวงกลม 1 ครั้ง
void setup()
{
size(300, 300); // Display window Size x(Horizontal), Size y(Vertical)
background(204); // Background color
line(10,20,10,150); // Draw line between x1,y1 to x2,y2 in the display window
line(100,50,200,150); // Draw line between x1,y1 to x2,y2 in the display window
ellipse(100, 120, 55, 75); // Draws an ellipse (oval) in the display window
ellipse(150, 200, 55, 55); // Draws a circle in the display window
}
void draw()
{
}
-----------------------------------------------------------------------------------------------------------------
ตัวอย่าง 1.2 เส้นตรงเคลื่อนที่เป็นวงกลม
float r = 100;
float x,y;
float d = 0;
void setup()
{
size(300, 300);
loop();
}
void draw()
{
background(204);
x = width/2 + r*cos(d);
y = height/2 + r*sin(d);
line(width/2, height/2, x, y);
delay(10);
d = d+0.01;
if(d >= TWO_PI)
{
d = 0;
}
}
-----------------------------------------------------------------------------------------------------------------
ตัวอย่างที่ 1.3 เส้นตรงหมุนตามเมาส์ โดยมีจุดศูนย์กลางอยู่ที่ x = 150 และ y = 150
ไฟล์ test_1.pde
void setup()
{
size(300, 300);
}
void draw()
{
background(204);
line(150, 150, mouseX, mouseY);
}
-----------------------------------------------------------------------------------------------------------------
อ้างอิง