Program for line translation

Translation of a line in computer graphics is changing the position of line in the graph.This picture shows the translation of a point.Here we are adding the translation points to the original points.If (x,y) are original points and translation points are (tx, ty) then


1-point translation(source-tutorialpoint.com)


                                        x'=x+tx;
                                         y'=y+ty;
similarly, in a line, we can translate both the end points (x1, y1) and (x2, y2).



//program for line translation

#include<bits/stdc++.h>

#include<graphics.h>

using namespace std;

int main()

{

    int gd,gm,x1,x2,y1,y2,tx,ty;

    //taking line end points as input

    cout<<"enter end points:"<<endl;

    cin>>x1>>y1>>x2>>y2;

    cout<<"Enter the translation co-ordinates:";

    cin>>tx>>ty;

    //intializing graph

    //keep in mind that always initialize graph after taking input

    detectgraph(&gd,&gm);

    initgraph(&gd,&gm,"");

    //draw line using line function

    line(x1,y1,x2,y2);

    line(x1+tx,y1+ty,x2+tx,y2+ty);

    //getch() will close the graph after entering a character

    getch();

return 0;

}


OUTPUT-

enter the end points- 20 20 100 100
Enter the translation co-ordinates-150,150

To execute the given code-

1-Save the code as translate_line.cpp
2-open terminal and type-
g++ translate_line.cpp -lgraph
and click enter.
3-execute ./a.out and see the results-

graph shown below is in first quadrant as vertical line is x-axis and horizontal line is y-axis



Comments :

Post a Comment