Diễn đàn sinh viên công nghệ thông tin, chia sẻ, giao lưu, học hỏi. Kết nối ... Những ngôn ngữ cơ bản mà bạn cần phải nắm nếu muốn thành 1 lập trình viên ...VuaTenMien.Com
Thứ Bảy, 17 tháng 8, 2013
Các thuật toán vẽ đoạn thẳng trong C/C++
1. Thuật toán vẽ đoạn thẳng thông thường:
void dline(int x1,int y1, int x2,int y2, int color) {
float y;
int x;
for (x=x1; x<=x2; x++) {
y = y1 + (x-x1)*(y2-y1)/(x2-x1) ;
putpixel(x, Round(y), color );
}
}
2. Thuật toán DDA (Digital Differential Analizer):
void ddaline (int x1,int y1,int x2,int y2,int c){
int x=x1;
float y=y1;
float k=(float)(y2-y1)/(x2-x1);
putpixel(x,round(y),c);
for(int i=x1;i<=x2;i++) {
x++;
y=y+k;
putpixel(x,round(y),c);
}
}
3. Thuật toán Bresenham
/*Thuat toan Bresenham ve dthang (0<k<1) */
void Bre_line(int x1, int y1, int x2, int y2, int c)
{int x, y, dx, dy,p,const1,const2;
y = y1;
dx = x2 - x1;
dy = y2 - y1;
p = 2*dy - dx;
const1 = 2*dy;
const2 = 2*(dy-dx);
for (x=x1; x<=x2; x++) {
putpixel(x, y, c);
if (p < 0)
p += const1; // p=p + 2dy
else {
p +=const2; //p=p+2dy-2dx
y++;
}
}
}
4. Thuật toán Trung điểm - Midpoint
/* Thuat toan Midpoint de ve doan thang (0<k<1) */
void Mid_line(int x1, int y1, int x2, int y2, int c){
int x, y, dx, dy,d;
y = y1;
dx = x2 - x1;
dy = y2 - y1;
d= dy - dx/2;
for (x=x1; x<=x2; x++){
putpixel(x, y, c);
if (d <= 0)
d = d + dy;
else {
y ++;
d = d + dy - dx;
}}
}
Đăng ký:
Đăng Nhận xét (Atom)
Bài đăng phổ biến
-
import java.io.*; public class Test_Ngto{ public static void main(String args[]){ InputStream is = System.in; ...
-
import java.io.*; public class ReadLine{ public static void main(String args[]){ InputStreamReader isr = new InputStreamReader...
-
1. Hãy xây dựng lớp Diem cùng với các đối tượng điểm trong mặt phẳng và các phương thức sau: - Phương thức khởi tạo - Phương...
-
<html> <head> <title>Giai phuong trinh bac 2</title> <script language="JavaScript"> function giai(fo...
-
import java.io.*; public class Test_Tng{ public static void main(String args[]){ InputStream is = System.in; ...
-
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespa...
-
<html> <head><script LANGUAGE="JavaScript"> function a_plus_b(form) { a=eval(form.a.value) ...
-
import java.io.*; public class Gptb2{ public static void main(String args[]){ InputStreamReader isr = new InputStream...
-
<html> <head> <title>giai phuong trinh bac 1</title> <script language="JavaScript"> function giai(fo...
-
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespa...

Không có nhận xét nào:
Đăng nhận xét