Showing posts with label KIST College. Show all posts
Showing posts with label KIST College. Show all posts

RGB to HSV Conversion

Input: RGB
Output: HSV


Steps:
1. Input RGB.
2. Normalize the RGB values to be in the range [0, 1].
    r = R/255, g = G/255, b = B/255
3. Find the difference between max and min values.
    M = max(r,g,b), m = min(r,g,b). And, the difference Δ = M - m
4. Calculate hue H.
    If M = m = 0, then H = 0
    If M = r, then compute H = (60 * ((g – b) / Δ) + 360) % 360
    If M = g, then compute H = (60 * ((b – r) / Δ) + 120) % 360
    If M = b, then compute H = (60 * ((r – g) / Δ) + 240) % 360
5. Calculate saturation S.
    If Δ = 0, S = 0; Else, S = Δ/M
6. Calculate value V.
    V = max(r,g,b)
7. Output HSV.

8086 Program - Sum of Two Input Numbers

This is an assembly language program for 8086 microprocessor in which user gives two data as input and the program finds its sum. Here, only one digit data is permitted. What we input here is in ASCII value, so we subtract by 48 before addition; and add by 48 after addition so that we can properly display the value in decimal value.

.model small 
.data
str1 db "Enter first number: $"
str2 db 13,10,"Enter second number: $"
str3 db 13,10,"The sum is: $"
.code
main proc
    mov ax,@data
    mov ds,ax
    lea dx,str1
    mov ah,9
    int 21h
    mov ah,1
    int 21h
    mov bl,al
    lea dx,str2
    mov ah,9
    int 21h
    mov ah,1
    int 21h
    mov bh,al
    sub bh,48
    sub bl,48
    lea dx,str3
    mov ah,9
    int 21h   
    add bh,bl
    add bh,48
    mov dl,bh
    mov ah,2
    int 21h
    mov ah,4Ch
    int 21h
main endp
end

8086 Program : Input String and Display Each Word in Next Line

.model small

.data
msg db 60 dup(?)

.code
main proc
    mov ax,@data
    mov ds,ax
    mov si,offset msg

input: mov ah,1
    int 21h
    cmp al,13
    je display
    mov [si],al
    inc si
    jmp input

display: mov [si],'$'
    mov di,offset msg
    mov dl,13
    mov ah,2
    int 21h
    mov dl,10
    mov ah,2
    int 21h

again: cmp [di],'$'
    je last
    cmp [di],32
    je next
    mov dl,[di]
    mov ah,2
    int 21h
    inc di
    jmp again

next: mov dl,13
    mov ah,2
    int 21h
    mov dl,10
    mov ah,2
    int 21h
    inc di
    jmp again

last: mov ah,4ch
    int 21h

main endp

end

How to construct a Bezier curve?

ALGORITHM:

Coordinate Representations in Computer Graphics

With few exceptions, general graphics packages are designed to be used with Cartesian coordinate specifications. If coordinate values for a picture are specified in some other reference frame (spherical, hyperbolic, etc.), they must be converted to Cartesian coordinates before they can be input to the graphics package. Special-purpose packages may allow use of other coordinate frames that are appropriate to the application. In general; several different Cartesian reference frames are used to construct and display a scene. We can construct the shape of individual objects, such as trees or furniture, in a scene within separate coordinate reference frames called modeling coordinates (MC), or sometimes local coordinates or master coordinates. Once individual object shapes have been specified, we can place the objects into appropriate positions within the scene using a reference frame called world coordinates (WC). Finally, the world-coordinate description of the scene is transferred to one or more output-device reference frames for display. These display coordinate systems are referred to as device coordinates (DC) or, screen coordinates in the case of a video monitor. Modeling and world-coordinate definitions allow us to set any convenient floating-point or integer dimensions without being hampered by the constraints of a particular output device. For some scenes, we might want to specify object dimensions in fractions of a foot, while for other applications we might want to use millimeters, kilometers, or light-years.

8086 ALP: Display STRING in the CENTRE of the screen

PRNT MACRO MSG
    MOV AH,09h
    LEA DX,MSG
    INT 21H
ENDM

DATA SEGMENT
    MSG1 DB "ABC PQR XYZ$"
DATA ENDS

CODE SEGMENT 
    ASSUME CS:CODE,DS:DATA
    START: 
    MOV AX,DATA
    MOV DS,AX
    MOV AX,0600H
    MOV BH,71H
    MOV CX,0000H ;UPPER LEFT ROW,COLUMN
    MOV DX,0184H ;LOWER RIGHT ROW,COLUMN
    INT 10H
    MOV AH,02H
    MOV BH,00H
    MOV DH,0CH
    MOV DL,23H
    INT 10H
    PRNT MSG1
    MOV AH,4CH
    INT 21H
CODE ENDS
    END START

OUTPUT:

Liang-Barsky Line Clipping Algorithm

  • For given window with (XWmin, YWmin) and (XWmax, YWmax), the given line has end-points (X1, Y1) and (X2, Y2).
  • Calculate 
  • Calculate pk, qk and rk for k = 1, 2, 3, 4 as:
  • If pk=0 for some k, then the line is parallel to clipping boundary. Now test qk:
If one qk < 0 for these k, then the line is outside. 
If all qk ≥ 0 for these k, then some portion of the line is inside.
  • For all pk < 0 (i.e. line proceeds from outside to inside the boundary), calculate 
    to determine intersection point with the possibly extended clipping boundary k and obtain a new starting point for the line at u1.
  • For all pk > 0 (i.e. line proceeds from inside to outside the boundary), calculate 
    to determine intersection point with the possibly extended clipping boundary k and obtain a new end point for the line at u2.
  • If
    discard the line. 
  • Else, the line is now between
  • Hence,
  • Updated points are

8085 ALP: ASCENDING ORDER in a block of DATA

  • Suppose, there is an array of ten 8-bit data stored in memory addresses from 2030h to 2039h.
  • Starting from 2030h, we compare the data of the first location with that of the second location.
  • If the data of the first location is greater than that of the second location, they are swapped.
  • Running external loop for 9 times, 8 times, 7 times and so on, we run every internal loop as 9 times, 8 times, 7 times and so on for every corresponding external loop. For e.g. for external loop 9, we run respective internal loop 9 times.
  • Doing so, after every external loop, the largest number will be at the last memory location.
MVI C, 09h
AGAIN: MOV D, C
LXI H, 2030h
NEXT: MOV A, M
INX H
CMP M
JC SKIP
MOV B, M
MOV M, A
DCX H
MOV M, B
INX H
SKIP: DCR D
JNZ NEXT
DCR C
JNZ AGAIN
HLT

Program to Find the Largest Number in a block of DATA: 8085

  • Suppose, there is an array or block of ten 8-bit data stored from memory address 2030h. And, we have to find out the greatest number among them.
  • What we are going to do here is that we compare every two data from the first location. If the data of first location is greater than that of second location, the latter will be replaced by the former. 
  • In this way, for 10 data, we run this logic 9 times and finally, the maximum number will be at the last location i.e, 2039h.


LXI H, 2030h
MVI C, 09h
AGAIN: MOV A, M
INX H
CMP M
JC SKIP
MOV M, A
SKIP: DCR C
JNZ AGAIN
HLT


Output:

Cohen-Sutherland Line Clipping Algorithm

1. Assign 4-bit REGION Code [TBRL] for end-points of line.
  • Bit 1 is set to 0 if XWmin≤x, else set to 1. (L)
  • Bit 2 is set to 0 if x≤XWmax, else set to 1. (R)
  • Bit 3 is set to 0 if YWmin≤y, else set to 1. (B)
  • Bit 4 is set to 0 if y≤YWmax, else set to 1. (T)
(Condition: XWmin≤x≤XWmax, YWmin≤y≤YWmax)

2. Determine whether the line is completely inside or outside of the window using tests.
a) If end-point has region code 0000, the point is completely inside.
b) If logical AND of end-points is not 0000, the line is completely outside.
c) If logical AND of end-points is 0000, the line is not completely outside.

3. If 2(c) exists, we need to find the intersection with window boundary.
Here, 

i) If bit 1 is 1, line intersects with left boundary.
So,
where x=XWmin.

ii) If bit 2 is 1, line intersects with right boundary.
So,
where x=XWmax.

iii) If bit 3 is 1, line intersects with bottom boundary.
So,
where y=YWmin.

iv) If bit 4 is 1, line intersects with top boundary.
So,
where y=YWmax.
Here, xi & yi are x & y intercepts for that line. Update that.

4. Repeat (1) to (3) for every points till completely accepted.

Line Clipping

The concept of line clipping is same as point clipping. In line clipping, we will cut the portion of line which is outside of window and keep only the portion that is inside the window.

Cohen-Sutherland Line Clipping Algorithm
This algorithm uses the clipping window as shown in the following figure. The minimum coordinate for the clipping region is (XWmin,YWmin) and the maximum coordinate for the clipping region is (XWmax,YWmax).
We will use 4-bits to divide the entire region. These 4 bits represent TBRL (the Top, Bottom, Right, and Left) of the region as shown in the following figure. Here, the TOP and LEFT bit is set to 1 because it is the TOP-LEFT corner.
There are 3 possibilities for the line −
  • Line can be completely inside the window (This line will be completely removed from the region).
  • Line can be completely outside of the window (This line will be completely removed from the region).
  • Line can be partially inside the window (We will find intersection point and draw only that portion of line that is inside region).

8085 ALP: Division of TWO 16-bit NUMBERS

;SUPPOSE, We are going to divide 2050h by 1630h.
LXI H,0000H
SPHL
;SP holds quotient.
LXI H,2050H
;HL holds the dividend.
LXI B,1630H
;BC holds the divisor.
AGAIN: MOV A,H
CMP D
JC LAST
JZ CHKNXT
JMP DIVIDE
CHKNXT: MOV A,L
CMP C
JNC DIVIDE
JMP LAST
;Up to this, we have compared dividend with divisor.
DIVIDE: INX SP
;Now, HL <- HL - BC = HL + ((complement of BC) + 1)
;HL <- HL + DE + 1
MOV A,B
CMA
MOV D,A
MOV A,C
CMA
MOV E,A
INX D
DAD HL
;Finally, HL holds the remainder.
JMP AGAIN
LAST: HLT

8085 ALP: Separate a 8-bit data into two NIBBLES

In 8-bit data, there are two nibbles. Nibble is of 4-bit. Here, we take 6BH as a 8-bit data which is separated into two nibbles 06H and 0BH and stored at 2030h and 2031h respectively.

MVI A,6Bh
MOV C,A
ANI 0Fh
STA 2030h ;0Bh is stored.
MOV A,C
ANI F0h
RRC
RRC
RRC
RRC
STA 2031h ;06h is stored.
HLT

8085 ALP: Finding the SQUARE ROOT of a Number

MVI B,01h
MVI C,01h
LDA 2030h
;Suppose the number is located at 2030h
AGAIN: SUB B
JZ LAST
INR B
INR B
INR C
JMP AGAIN
LAST: MOV A,C
STA 2031h
;the square root is stored at 2031h
HLT

DDA (Digital Differential Analyzer) line drawing algorithm in Graphics

#include <graphics.h>
#include <stdio.h>
#include <math.h>

float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i;

void main( )
{
  int gd = DETECT,gm;
  initgraph(&gd,&gm,"c:\\tc\\bgi");
  printf("Enter the value of x1 : ");
  scanf("%f",&x1);
  printf("Enter the value of y1 : ");
  scanf("%f",&y1);
  printf("Enter the value of x2 : ");
  scanf("%f",&x2);
  printf("Enter the value of y2 : ");
  scanf("%f",&y2);
  
  dx=abs(x2-x1);
  dy=abs(y2-y1);

  if(dx>=dy)
    pixel=dx;
  else
    pixel=dy;
  
  dx=dx/pixel;
  dy=dy/pixel;

  x=x1;
  y=y1;
  
  i=1;
  while(i<=pixel)
  {
    putpixel(x,y,1);
    x=x+dx;
    y=y+dy;
    i=i+1;
    delay(100);
  }
  getch();
  closegraph();
}


OUTPUT: