We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is a solution with integer numbers. using distance between points and cross product.
#include<bits/stdc++.h>usingnamespacestd;intsgn(intx){if(x==0)return0;return(x<0?-1:1);}structpoint{intx,y;point():x(0),y(0){}point(inta,intb):x(a),y(b){}booloperator==(constpoint&p)const{return(x==p.x&&y==p.y);}voidoperator*=(constintk){this->x*=k;this->y*=k;}pointoperator-(constpoint&p)const{returnpoint(x-p.x,y-p.y);}pointoperator+(constpoint&p)const{returnpoint(x+p.x,y+p.y);}pointrotate(){returnpoint(-y,x);}intdistance(constpoint&p)const{return(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);}intcross(constpoint&p)const{returnx*p.y-p.x*y;}};intn,m,radius;pointcenterCircle;vector<point>rectangule(5);boolisInCircle(constpoint&p){return(p.distance(centerCircle)<=radius*radius);}voidbuildRectangule(intx1,inty1,intx2,inty2){pointcenterRectangule((x1+x2)/2,(y1+y2)/2);rectangule[0]=point(x1,y1);rectangule[1]=centerRectangule+(rectangule[0]-centerRectangule).rotate();rectangule[2]=point(x2,y2);rectangule[3]=centerRectangule+(rectangule[2]-centerRectangule).rotate();rectangule[4]=rectangule[0];}boolisInRectangule(constpoint&p){intsgns[4];for(inti=0;i<4;++i){pointvector1=rectangule[i+1]-rectangule[i];pointvector2=p-rectangule[i];sgns[i]=sgn(vector1.cross(vector2));}// the sign of parallel edges have to be the same or 0return(sgns[0]*sgns[2]>=0&&sgns[1]*sgns[3]>=0);}intmain(){intx1,y1,x2,y2;cin>>m>>n;cin>>centerCircle.y>>centerCircle.x>>radius;centerCircle*=10;radius*=10;cin>>y1>>x1>>y2>>x2;buildRectangule(x1*10,y1*10,x2*10,y2*10);pointcurrent;for(intx=0;x<n;++x){for(inty=0;y<m;++y){current=point(x,y);current*=10;if(isInCircle(current)||isInRectangule(current))cout<<"#";elsecout<<".";}cout<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A Circle and a Square
You are viewing a single comment's thread. Return to all comments →
Here is a solution with integer numbers. using distance between points and cross product.